【微软编程一小时】题目1 : Arithmetic Expression
描写叙述
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的更多相关文章
- 微软编程一小时 题目2: Longest Repeated Sequence
题目2 : Longest Repeated Sequence 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of i ...
- Longest Repeated Sequence【微软编程一小时-题目2】
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of integers, A = a1, a2, ... an. A c ...
- 编程一小时 code.org [六一关注]
编程一小时活动的组织者是Code.org, 它是一个面向公众的公益组织,致力于在更多的学校推广计算机科学教育,并为女性和就业率低的有色人种学生学习计算机的机会.同时,一个空前强大的合作伙伴联盟也在支持 ...
- Programming pearls 编程珠玑的题目
Programming pearls 编程珠玑的题目 这段时间有空都在看编程珠玑,很经典的一本书,一边看一边用 python 做上面的题目,我做的都放到 github 上了 https://githu ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- C++编程显示四则运算题目
题目:C++编程显示四则运算题目 设计思路:(1)让用户自己确定出题的数量,同时显示加减乘除四则运算. (2)考虑到用户可能只会一种运算,因此可以选择运算.
- leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation
leetcode 逆波兰式求解 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid ope ...
- hihocoder Arithmetic Expression【在线查询】
Arithmetic Expression 时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you ...
- 结对编程(四则运算题目生成器core第七组)对接心得
在这篇博客博主想记录一下此次结队编程作业中与ui组对接的心得.在这里我也想表达一下对涂涵越同学的敬佩,他遇到困难时孜孜不倦求解的毅力着实让我佩服,我们在dll的生成上遇到了很大的困难,要不是他的坚持我 ...
随机推荐
- Apache RocketMQ在linux上的常用命令
Apache RocketMQ在linux上的常用命令 进入maven安装后的rocketmq的bin目录 1.启动Name Server 2.启动Broker 3.关闭Name Server 4 ...
- java的四个基本特征
现实生活中的事物被抽象成对象,把具有相同属性和行为的对象被抽象成类,再从具有相同属性和行为的类中抽象出父类. 封装 隐藏对象的属性和实现细节,仅仅对外公开接口. 封装的有优点: 1.便于使用者正确.方 ...
- Visual Studio 代码风格约束
团队内部若能统一代码风格对于日后的项目维护大有裨益,但面对厚达十几甚至几十页的代码风格规范,开发人员难免产生抵触心理.Python和Go等在语言层面就对代码风格作了一定的约束,但C#并没有,为解决这个 ...
- Linux常用基本命令:三剑客命令之-sed
sed是一个很强大的文件处理工具,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作 格式:sed [option] [command] [file] 常用命令: a ∶新 ...
- regular.js
//手机号验证 var regMobile=/^1[3,5,8]\d{9}$/; //固定电话 var regPhone=/^(^0\d{2}-?\d{8}$)|(^0\d{3}-?\d{7}$)|( ...
- Python 类的祖宗--metaclass
1.Python 中一切事物都是对象 2.类都是 type 类的对象 类的两种申明方法 # 方法一: class Foo: def func(self): print(666) obj = Foo() ...
- UED与UCD
UED User Experience Design(用户体验设计),简称UED.UED是以用户为中心的一种设计手段,以用户需求为目标而进行的设计.设计过程注重以用户为中心,用户体验的概念从开发的最早 ...
- CSS--居中方式总结
一.水平居中方法 1.行内元素.字体的水平居中 1.对于行内元素(display值为inline或inline-block都可以)或者字体:父元素添加css规则:text-align:center; ...
- Oracle绑定变量在C#.NET中的应用及意义
一. 什么是绑定变量 绑定变量(bind variable) : select * from emp where empno=:empno; 是用户放入查询中的占位符,它会告诉Oracle“我会随后为 ...
- SQLite 知识摘要 --- 事务
在许多时候,我们在使用大数据的时候会发现,尽管sqlite数据库的执行效率已经很快了,但是还是满足不了我们的需求,这时候我们会很容易考虑到使用并发的方式去访问sqlite数据库,但是sqlite数据独 ...