POJ1730 Perfect Pth Powers
Description
We say that x is a perfect square if, for some integer b, x = b2. Similarly, x is a perfect cube if, for some integer b, x = b3. More generally, x is a perfect pth power if, for some integer b, x = bp. Given an integer x you are to determine the largest p such that x is a perfect pth 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 pth power.
Sample Input
17 1073741824 25 0
Sample Output
1 30 2
View Code
1 #include<stdio.h> 2 #include<math.h> 3 int main(){ 4 int n; 5 while(scanf("%d",&n) , n){ 6 int flag = 1; 7 if(n<0){ 8 flag = -1; 9 } 10 int ans = 1,b,p; 11 for(int p=32;p>1;p--){ 12 b = (int)(pow(n*1.0*flag,1.0/p)+ 1e-8 )*flag ; 13 int temp = 1; 14 for(int i=0;i<p;i++) 15 temp *= b; 16 if(temp == n){ 17 ans = p ; 18 break; 19 } 20 } 21 printf("%d\n",ans) ; 22 } 23 return 0; 24 }