【微软编程一小时】题目1 : Arithmetic Expression
2014-04-28 18:54
567 查看
时间限制:2000ms
单点时限:200ms
内存限制:256MB
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.
样例输入
样例输出
每次都很傻逼地用 == 去判断String相等,每次都吃亏都不长记性!叫你不长记性!
单点时限:200ms
内存限制:256MB
描述
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.样例输入
4 901 / 100 3 * 3 2 + 6 8 - -1
样例输出
2
每次都很傻逼地用 == 去判断String相等,每次都吃亏都不长记性!叫你不长记性!
import java.util.Scanner; public class Main { static int InversionCount ; public static void main(String[] args) { int T,t; Scanner jin = new Scanner(System.in); T = jin.nextInt(); jin.nextLine(); int ret = T+1; double max_abs = Double.MAX_VALUE; for (t = 1; t <= T; t++) { String line = jin.nextLine(); String[] argStrings = line.split(" "); //System.out.println(argStrings.length); double a = Double.parseDouble(argStrings[0]); double b = Double.parseDouble(argStrings[2]); double op_ret ; if (argStrings[1].equals("+")) { op_ret = a + b; } else if (argStrings[1].equals("-")) { op_ret = a - b; } else if (argStrings[1].equals("*")) { op_ret = a * b; } else op_ret = a / b; if (Math.abs(op_ret - 9) < max_abs) { max_abs = Math.abs(op_ret - 9); ret = t; } } System.out.println(ret); } }
相关文章推荐
- 2014微软编程一小时 题目1 : Arithmetic Expression Java实现
- 微软编程一小时 题目2: Longest Repeated Sequence
- 微软编程一小时题目一
- 2014微软编程一小时题目2 : Longest Repeated Sequence
- 算法 Longest Repeated Sequence(最大重复子序列 微软编程一小时 题目2)
- 微软编程一小时题目二
- 2014微软编程一小时题目1 : Arithmetic Expression
- 微软编程一小时 题目1 : Arithmetic Expression
- 微软编程一小时 题目2 : Longest Repeated Sequence
- Longest Repeated Sequence【微软编程一小时-题目2】
- 微软编程一小时活动 - 暨微软2014实习生招募编程模拟测试 题目1 : Arithmetic Expression 原创解答
- 微软编程一小时题目1 : Arithmetic Expression
- 微软编程一小时比赛--题目1 : Arithmetic Expression
- 微软2014编程之美初赛第一场——题目2 : 树
- 科技活动周:微软“编程一小时”走进临沂
- 微软2014编程之美初赛第一场——题目3 : 活动中心
- 微软2014编程之美初赛第一场——题目2 : 树
- 微软2014实习生及校招秋令营技术类职位,在线编程题目
- 微软推出《我的世界》“编程一小时”免费教程,携手Code.org普及计算机科学教育
- 微软编程一小时--Longest Repeated Sequence