本文共 1681 字,大约阅读时间需要 5 分钟。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10067 | Accepted: 5372 |
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 35 327 3750 51000 52000 53000 51000000 50 0
Sample Output
123444516
Source
#include#include #include int main(void){ int min, tmp; int n, b; float right; int low, high; while (scanf("%d%d", &b, &n), n != 0 || b != 0){ if (n == 1 || b == 1){ printf("%d\n", b); continue; } right = pow(b, 1.0 / n); low = (int)right; high = (int)(right + 0.9999); min = b - pow(low, n); tmp = pow(high, n) - b; if (tmp < min){ printf("%d\n", high); } else{ printf("%d\n", low); } } return 0;}
转载地址:http://ploji.baihongyu.com/