代码改变世界

poj 1730 Perfect Pth Powers -----数论

2012-03-31 11:03  java环境变量  阅读(231)  评论(0)    收藏  举报

Perfect Pth Powers
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13214   Accepted: 3013

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

                

          题目大意:  输入一个数 n,  n可以表示成 x的y次方。输出最大的y。  如 输入25, 25可以是5的2次方,也可以是25的1次方。  输出大的,输出2.

          思路:  这道题 我在网上 查了 pow函数的一些资料。  之前一直 不知道怎么开方,  哎  ,直接  1/x 次方 不就是开x次方么。傻逼了。   然后开始做这题 ,以为可以水过。。但还是发现了疑问,已经写了文章向大家请教。。

  这题 我的思路是 先把 输入的数n开 方 ,求出n的i次方是m,i从 32开始 测试,直到最小的1.  但 是 pow函数 返回的是浮点型, 转化为整形会有偏差。  所以我再检验  m的i次方是否等于 n即可。  这题还得注意 n可能是负数。

//Memory: 428K		Time: 0MS
//Language: G++		Result: Accepted
#include<stdio.h>
#include<math.h>
int main()
{
	int n,i,t1,t2,z;
	while(scanf("%d",&n)!=EOF&&n)
	{
		if(n>0)
		   z=1;
		else z=-1;
		for(i=32;i>=1;i--)
		{
			if(z==-1&&i%2==0) continue;  //n如果为负数,不用考虑偶数次方。
		    t1=z*pow(z*n+0.1,1.0/i);  //先开方 。这里 我是加了0.1才过的,此题还是有疑问。
		    t2=pow(t1,i);          //再检验
		    if(t2==n)
			{
	   		   	printf("%d\n",i); break;
			}
		}
	}
	return 0;
}

    我关于pow的疑问:http://blog.csdn.net/zsc1993916/article/details/7414685    。 朋友们如果有时间帮我看看。