【发布时间】:2014-04-03 18:52:11
【问题描述】:
我有一个编程比赛即将举行,我正在解决去年的问题作为比赛的修订,我遇到了一个简单的程序,但是它需要数学,不幸的是我很不擅长。
问题来了:
给定一个正整数n,求奇数o和 非负整数 p 使得 n=o2^p (o 乘以 2 权力p)
输入文件的第一行正好包含一个正整数 d 等于测试用例的数量,1。数据集如下。每个数据集仅包含一行 正好是一个整数n, 1
样本输出
2 //d 测试用例个数的值
24 // n 的值
27 // n 的值样本输出
3 3 // 第一个 3 是 o 的值,第二个 3 是 p 的值
7 7 // 第一个 7 是 o 的值,第二个 7 是 p 的值“//”部分不应在输出或输入中
这就是我到目前为止所做的,除了我需要用于正确求解方程的公式之外,我做的一切都是正确的
public static void main(String[] args) {
double n = 0, d = 0, o = 0, p = 0;
double x = 1;
//START INPUT
Scanner input = new Scanner(System.in);
//input d, number of times program repeats
System.out.println("Please enter the number of times you want to run the test(no more than 10)");
d = input.nextDouble();
//check the validity of d
while((d > 10) || (d<1)){
System.out.println("Invalid input");
System.out.println("Enter another value for d");
d = input.nextDouble();
}
while(x <= d){
x++;
System.out.println("enter a value for n");
n = input.nextDouble();
//check the validity of n
while((n > 1000000) || (n<1)){
System.out.println("Invalid input.");
System.out.println("Enter Another Value for n");
n = input.nextDouble();
}
//Calculates
p = Math.log(n) / Math.log(2.0);
o = n / Math.pow(p, 2);
//STOP CALCULATE
//PRINTS
System.out.println(o + " " + p);
}
}
感谢任何帮助。
【问题讨论】:
-
27 的结果实际上不会是 (7,7)。作为另一个示例,您可以添加 28 和 (7,2)。