Arithmetic Expression
来源:互联网 发布:vmware12安装mac os x 编辑:程序博客网 时间:2024/06/16 18:19
时间限制:2000ms
单点时限:200ms
内存限制:256MB
- 样例输入
4901 / 1003 * 32 + 68 - -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.
0 0
- Arithmetic Expression
- OOD - Arithmetic Expression Tree
- XQuery表达式之算术表达式(Arithmetic Expression)
- 微软编程一小时 题目1 : Arithmetic Expression
- 微软编程一小时题目1 : Arithmetic Expression
- 【微软编程一小时】题目1 : Arithmetic Expression
- sicily 递归练习 1005. Arithmetic Expression Evaluation
- Codeforces 115E Unambiguous Arithmetic Expression
- MSSQL: Arithmetic overflow error converting expression to data type int
- 微软编程一小时比赛--题目1 : Arithmetic Expression
- 2014微软编程一小时 题目1 : Arithmetic Expression Java实现
- 2014微软编程一小时题目1 : Arithmetic Expression
- Arithmetic overflow error converting expression to data type int.”
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- Evaluate the value of an arithmetic expression in Reverse Polish Notation.
- 微软编程一小时活动 - 暨微软2014实习生招募编程模拟测试 题目1 : Arithmetic Expression 原创解答
- java-------连接数据库(jdbc)
- Java设计模式之从[暗黑破坏神"装备镶嵌宝石系统"]分析桥接(Bridge)模式
- Linux学xí笔记之——普通用户使用sudo命令
- Longest Repeated Sequence
- (9)Struts2的输入校验
- Arithmetic Expression
- 网站用户行为数据统计与分析之四:logstash及其配置
- 黑马程序员-html之表单组件<form>,下拉菜单<select>,文本域<textarea>
- PHP基础之字符串比较
- .Net4.0 安装时发生严重错误,无法安装
- android 编程技巧总结
- ffmpeg 转AAC时出现Unable to find a suitable output format for
- memcache在游戏中的应用
- 《大型网站技术架构》读书笔记 (转载请注明来源:http://zhanjia.iteye.com/blog/2038540)