您的位置:首页 > 编程语言 > Java开发

poj2109 Power of Cryptography(java)

2017-08-19 14:03 519 查看
package test;

import java.util.Scanner;

/**问题请参考http://poj.org/problem?id=2109
 * @author rayli

 * @date:2014-7-22 下午2:04:08
 * 题意 :求一个整数k,使得k满足k^n=p
 *
 */
public class PowerCryptography
{
    boolean Binarysearch(double n, double p)
    {
        //二分查找
        int num = (int)(p / n);
        int start = 1;
        int end = (int)Math.pow(10, 9);
        
        while(start <= end)
        {
            int mid = (end + start) / 2;
            double tmp = Math.pow(mid, n);
            
            if(tmp < p)
            {
                start = mid + 1;
            }
            else if(tmp > p)
            {
                end = mid -1;
            }
            else
            {
                System.out.println(mid);
                return true;
            }
        }
        
        return false;
    }
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        PowerCryptography pc = new PowerCryptography();
        
        double n = cin.nextDouble();
        double p = cin.nextDouble();
        while(true)
        {  
            if(n > 0 && p > 0)
            {
                pc.Binarysearch(n, p);
                n = cin.nextDouble();
                p = cin.nextDouble();
            }
            else
                break;

        }
        cin.close();
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: