ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1001. Reverse Root

Whats wrong with this code? Please help
Posted by Naman Sharma 31 Jan 2020 21:07
#include<iostream>
using namespace std;
float sqrt(long long2 n,int p)
{
    int start = 0;
    int end = n;
    float ans;
    int mid;
    while(start <= end)
    {
        mid = (start+end)/2;
        if(mid*mid < n)
            start = mid + 1;
        else if(mid*mid > n)
            end = mid - 1;
        else
        {
            ans = mid;
            break;
        }
    }
    float increment = 0.1;
    for(int i = 0; i < p;i++)
    {
        while(ans * ans <= n)
        {
            ans += increment;
        }
        ans -= increment;
        increment /= 10;
    }
    return ans;
}
int main()
{
    long long int n;
    while(cin >> n)
    {
        cout << sqrt(n,4) << endl;
    }
}
Re: Whats wrong with this code? Please help
Posted by ToadMonster 7 Feb 2020 13:12
1) Please read task, especially "from the last one till the first" carefully. Look at the example.
2) Your sqrt function lies. https://ideone.com/wcJVz0