时间限制:
2000ms
单点时限:
200ms
内存限制:
256MB
-
4 901 / 100 3 * 3 2 + 6 8 - -1
样例输出
-
2
我的代码,仅供参考:
-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int count = input.nextInt();
if (count < 1 || count > 50000)
return;
double[] result = new double[count];
int i = 0;
// System.out.print("count:"+count);
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
if (line.contains("*")) {
String num[] = line.split("\\*");
int a = Integer.parseInt(num[0]);
int b = Integer.parseInt(num[1]);
if (a < -10000 || a > 10000)
return;
if (b < -10000 || b > 10000)
return;
double result_temp = a * b;
result[i - 1] = Math.abs(result_temp - 9.0);
} else if (line.contains("/")) {
String num[] = line.split("\\/");
int a = Integer.parseInt(num[0]);
int b = Integer.parseInt(num[1]);
if (a < -10000 || a > 10000)
return;
if (b < -10000 || b > 10000)
return;
double result_temp = a / b;
result[i - 1] = Math.abs(result_temp - 9.0);
} else if (line.contains("+")) {
String num[] = line.split("\\+");
int a = Integer.parseInt(num[0]);
int b = Integer.parseInt(num[1]);
if (a < -10000 || a > 10000)
return;
if (b < -10000 || b > 10000)
return;
double result_temp = a + b;
result[i - 1] = Math.abs(result_temp - 9.0);
} else if (line.contains("-")) {
int p = line.indexOf("-");
if (p == 0) {
String temp_str = line;
temp_str = temp_str.substring(1);
// System.out.println(temp_str);
p = temp_str.indexOf("-") + 1;
String a_s = line.substring(0, p);
String b_s = line.substring(p + 1);
int a = Integer.parseInt(a_s);
int b = Integer.parseInt(b_s);
if (a < -10000 || a > 10000)
return;
if (b < -10000 || b > 10000)
return;
double result_temp = a - b;
result[i - 1] = Math.abs(result_temp - 9.0);
} else {
String a_s = line.substring(0, p);
String b_s = line.substring(p + 1);
int a = Integer.parseInt(a_s);
int b = Integer.parseInt(b_s);
if (a < -10000 || a > 10000)
return;
if (b < -10000 || b > 10000)
return;
double result_temp = a - b;
result[i - 1] = Math.abs(result_temp - 9.0);
}
}
if (i == count) {
break;
}
i++;
}
int minIndex = 0;
double min;
min = result[0];
for (int index = 1; index < result.length; index++) {
if (result[index] < min) {
min = result[index];
minIndex = index;
}
}
System.out.println(minIndex + 1);
}
}
描述
Given N arithmetic expressions, can you tell whose result is closest to 9?
输入
Line 1: N (1 <= N <= 50000).
Line 2..N+1: Each line contains an expression in the format of "a op b" where a, b are integers (-10000 <= a, b <= 10000) and op is one of addition (+), subtraction (-), multiplication (*) and division (/). There is no "divided by zero" expression.
输出
The index of expression whose result is closest to 9. If there are more than one such expressions, output the smallest index.