首页 > 专题系列 > Java解POJ > POJ 1730 Perfect Pth Powers [解题报告] Java
2013
11-10

POJ 1730 Perfect Pth Powers [解题报告] Java

Perfect Pth Powers

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

17
1073741824
25
0

样例输出:

1
30
2

解题代码:

//* @author: Yeming Hu"cslittleye@gmail.com"
import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
	while(true)
	{
	    long x = sc.nextLong();
	    long xx = Math.abs(x);
	    if(x == 0)
	    {
	        break;
	    }
            int result = 0;
	    for(int i = 32; i >= 1; i--)
	    {
	        if(i%2 == 0 && x < 0)
		{
		    continue;
		}
	        double m = Math.pow(xx,1.0/i);
		int n1 = (int)Math.floor(m);
		int n2 = (int)Math.ceil(m);
		//System.out.println(i+ " " + m + " " + n1 + " " + n2);
	        if(Math.pow(n1,i) == xx || Math.pow(n2,i) == xx)
		{
		    result = i;
		    break;
		}
	    }
	    System.out.println(result);
	}
    }
}

  1. 第23行:
    hash = -1是否应该改成hash[s ] = -1

    因为是要把从字符串s的start位到当前位在hash中重置

    修改提交后能accept,但是不修改居然也能accept