POJ 1730 Perfect Pth Powers

Perfect Pth Powers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17339   Accepted: 3974

Description

We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if, for some integer b, x = b 3. More generally, x is a perfect pth power if, for some integer b, x = b p. Given an integer x you are to determine the largest p such that x is a perfect p th power.

Input

Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.

Output

For each test case, output a line giving the largest integer p such that x is a perfect p th power.

Sample Input

17
1073741824
25
0

Sample Output

1
30
2


题目大意:要求出一个完美的平方数,完美的平方数是这样的,n = bp的当P最大的时候才是一个完美的平方数

解题思路:将P从31到1遍历枚举,使用POW函数将他求出来,不过有一点问题就是精度问题,当用POW(125,1/3),直接取整的时候是4,所以需要在后面加一个0.1,也就是(int)(POW(125,1/3)+0.1).这样就可以求出结果了,还有要注意的是,他给出的N可能是负数,所以要对负数特殊处理,也就是说当N是负数的时候,P不可能是偶数,只能是奇数。

#include <cstdio>
#include <cmath>
using namespace std;
//typedef long long ll;

//ll x;
int x;
int main()
{
    while (scanf("%d", &x), x){
        if (x > 0){
            for (int i = 31; i>= 1; i--){
                int a = (int)(pow(x * 1.0, 1.0 / i) + 0.1);
                int b = (int)(pow(a * 1.0, 1.0 * i) + 0.1);
                if (x == b){
                    printf("%d\n", i);
                    break;
                }
            }
        }
        else{
            x = -x;
            for (int i = 31; i>= 1; i -= 2){
                int a = (int)(pow(x * 1.0, 1.0 / i) + 0.1);
                int b = (int)(pow(a * 1.0, 1.0 * i) + 0.1);
                if (x == b){
                    printf("%d\n", i);
                    break;
                }
            }
        }
    }
    return 0;
}


你可能感兴趣的:(POJ 1730 Perfect Pth Powers)