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();
}
}
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();
}
}
相关文章推荐
- POJ 2109 Power of Cryptography
- poj——2109——Power of Cryptography
- poj 2109 -- Power of Cryptography
- POJ 2109 Power of Cryptography
- poj 2109 Power of Cryptography
- 【二分答案】POJ-2109 Power of Cryptography
- POJ-2109 Power of Cryptography
- POJ 2109 Power of Cryptography
- POJ 2109 *** Power of Cryptography
- POJ 2109 Power of Cryptography
- poj 2109 Power of Cryptography(暴力枚举)
- Power of Cryptography - poj 2109
- POJ 2109 Power of Cryptography
- POJ2109 ZOJ2486 UVA113 UVALive3098 Power of Cryptography【数学】
- poj 2109 Power of Cryptography
- POJ 2109 Power of Cryptography
- poj 2109 Power of Cryptography
- POJ 2109 - Power of Cryptography
- POJ 2109 Power of Cryptography 大数,二分,泰勒定理 难度:2
- 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)