码迷,mamicode.com
首页 > 其他好文 > 详细

【微软编程一小时】题目1 : Arithmetic Expression

时间:2019-03-04 18:55:27      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:can   span   article   ide   spl   hose   visio   als   java   

时间限制:2000ms
单点时限: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);
	}
}



【微软编程一小时】题目1 : Arithmetic Expression

标签:can   span   article   ide   spl   hose   visio   als   java   

原文地址:https://www.cnblogs.com/mqxnongmin/p/10472186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!