The question is:
You are given 2 numbers (N , M); the task is to find N√M (Nth root of M).
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two space separated integers N and M.
Output:
For each test case, in a new line, print an integer denoting Nth root of M if the root is an integer else print -1.
Now my solution to this problem was:
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int t;
float x, p;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>p>>x;
if(p==0)
{
cout<<"1"<<endl;
}
else
{
float res=pow(x,(1/p));
cout<<"res="<<res<<endl;
if(res==int(res))
cout<<res<<endl;
else
cout<<"-1"<<endl;
}
}
return 0;
}
This caused a problem in the test case :
1 3 1000
Although when I printed res
, I got 10
as a result, during the condition checking if(res==int(res))
turned out to be false.
Also I noticed that changing from float res=pow(x,(1/p));
to float res=pow(x,(1.0/p));
was giving the correct answer. I'm guessing its something related to getting 0.33333
when the evaluation of 1/p
is done, but I cannot understand why the printed value is 10
but not matching in the condition checking.