Perfect PTH powerstime limit: 1000 ms memory limit: 10000 ktotal submissions: 16699 accepted: 3786 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 magn=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
Source
Waterloo local 2004.01.31
For integer B, n = B ^ P (B is a positive integer), if p is the maximum, n is the perfect number of integers.
Returns the maximum P value of the number n when the number n is the perfect limit.
Idea: P traverses from 31 to 1, calculates the p-start of N, converts it to t of int type, and then returns the P power of T to the X of int type.
If X and n are equal, the obtained p is the maximum value, and the break output Loop
Note: Evaluate the P times of N using POW (), because the POW () function obtains the double type, while the double type data
Accuracy problems. For example, 4 can be expressed as 3.99999 ...... Or 4.0000001, so when it is converted to int type + 0.1
Reference blog: http://blog.csdn.net/wangjian8006/article/details/7831478
#include<stdio.h>#include<math.h>int main(){ int n; while(~scanf("%d",&n) && n) { if(n > 0) { for(int i = 31; i >= 1; i--) { int t = (int)(pow(n*1.0,1.0/i) + 0.1); int x = (int)(pow(t*1.0,1.0*i) + 0.1); if(n == x) { printf("%d\n",i); break; } } } else { n = -n; for(int i = 31; i >= 1; i-=2) { int t = (int)(pow(n*1.0,1.0/i) + 0.1); int x = (int)(pow(t*1.0,1.0*i) + 0.1); if(n == x) { printf("%d\n",i); break; } } } } return 0;}
Poj1730_perfect PTH powers]