首页 > 代码库 > POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)

POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)

题目链接:

POJ:http://poj.org/problem?id=3100

ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1818
HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2740


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.

Sample Input

4 3
5 3
27 3
750 5
1000 5
2000 5
3000 5
1000000 5
0 0

Sample Output

1
2
3
4
4
4
5
16

Source

Mid-Central USA 2006

题意:

给定整数b和n,求整数a使得a^n最接近b。

代码如下:

#include <cstdio>
#include <cmath>
int main()
{
    int b, n;
    while(~scanf("%d%d",&b,&n))
    {
        if(b==0 && n==0)
            break;
        double tt = (double)pow(b,1.0/n);
        int t1 = ceil(tt);//向上取整
        int t2 = floor(tt);//向下取整
        if(b-pow(t2,n)>pow(t1,n)-b)
            printf("%d\n",t1);
        else
            printf("%d\n",t2);
    }
    return 0;
}


POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)