标签:
Description
Given positive integers B and N, find an integer A such that AN is as close as possible to B. (The result A is an approximation to the Nth root of B.) Note that AN may be less than, equal to, or greater than B.
Input: The input consists of one or more pairs of values for B and N. Each pair appears on a single line, delimited by a single space. A line specifying the value zero for both B and N marks the end of the input. The value of B will be in the range 1 to 1,000,000 (inclusive), and the value of N will be in the range 1 to 9 (inclusive).
Output: For each pair B and N in the input, output A as defined above on a line by itself.
| Example Input: | Example Output: |
| 4 3 5 3 27 3 750 5 1000 5 2000 5 3000 5 1000000 5 0 0 |
1 2 3 4 4 4 5 16 |
<span style="font-size:14px;">#include<stdio.h>
#include<math.h>
int main()
{
int b,n;
while(scanf("%d %d",&b,&n),b,n)
{
int i, temp1, temp2;
for(i=1;i<=b;i++)
{
if(int(pow((float)i,(int)n))==b)<span style="color:#33CC00;">//注意b是整形,所以要进行强制转换</span>
{
printf("%d\n",i);
break;
}
else if(pow((float)i,(int)n)<b&&pow((float)i+1,(int)n)>b)
{
temp1=pow((float)i,(int)n);
temp2=pow((float)i+1,(int)n);
if(temp1-b>0&&temp2-b>0)
{
if(temp1-b<temp2-b)
{
printf("%d\n", i);
break;
}
else
{
printf("%d\n", i+1);
break;
}
}
else if(b-temp1>0&&b-temp2>0)
{
if(b-temp1<b-temp2)
{
printf("%d\n", i);
break;
}
else
{
printf("%d\n", i+1);
break;
}
}
else if(b-temp1>0&&temp2-b>0)
{
if(b-temp1<temp2-b)
{
printf("%d\n", i);
break;
}
else
{
printf("%d\n", i+1);
break;
}
}
else if(temp1-b>0&&b-temp2>0)
{
if(temp1-b<b-temp2)
{
printf("%d\n", i);
break;
}
else
{
printf("%d\n", i+1);
break;
}
}
}
}
}
return 0;
}</span>这个题要会灵活运用数学函数,在此题中就出现了一个pow()———幂函数,其实此题还可以进一步简化,要用到abs()——绝对值函数,但是能力有限,用的不对,所以,才这么麻烦。。。。。。,哎,还需要努力呀!标签:
原文地址:http://blog.csdn.net/unusualnow/article/details/43458111