ACM Arithmetic Expression
Description
Given N arithmetic expressions, can you tell whose result is closest to 9?
Input
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.
Output
The index of expression whose result is closest to 9. If there are more than one such expressions, output the smallest index.
Sample Input
4
901 / 100
3 * 3
2 + 6
8 - -1
Sample Output
2
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iterator>
using namespace std; int main(){
int n;
cin >> n;
vector<double> res(n,);
for(int i = ; i < n; ++ i){
double a,b;
char op;
cin >> a >> op >> b;
switch(op){
case '+':
res[i] = fabs(a+b-);
break;
case '-':
res[i] = fabs(a-b-);
break;
case '*':
res[i] = fabs(a*b-);
break;
case '/':
res[i] = fabs(a/b-);
break;
default:
break;
}
}
cout<<distance(res.begin(), min_element(res.begin(),res.end()))+<<endl;
return ;
}
ACM Arithmetic Expression的更多相关文章
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- 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 ...
- Arithmetic Expression
时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you tell whose result is cl ...
- 【微软编程一小时】题目1 : Arithmetic Expression
时间限制:2000ms 单点时限:200ms 内存限制:256MB 描写叙述 Given N arithmetic expressions, can you tell whose result is ...
- Variables and Arithmetic Expression
Notes from The C Programming Language A decimal point in a constant indicates that it is floating po ...
- 【Codeforces 115D】Unambiguous Arithmetic Expression
Codeforces 115 D 题意:给一个没有括号的表达式,问有多少种添加括号的方法使得这是一个合法的表达式?输入可能有正负号.加减乘除.数字. 思路1: 这是不能过的\(naive\)的\(dp ...
- ACM学习历程——UVA11234 Expressions(栈,队列,树的遍历,后序遍历,bfs)
Description Problem E: Expressions2007/2008 ACM International Collegiate Programming Contest Unive ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
随机推荐
- Enter password for default keyring to unlock
file /home/ok/.gnome2/keyrings/login.keyring /home/ok/.gnome2/keyrings/login.keyring: GNOME keyring, ...
- CLR via C#(15)--String,熟悉而又陌生
好久没写文章了,再拿起这本书,学习加分享,乐趣无穷啊.这两天看了写关于字符串的知识,从学写代码的时候开始,我们就基本天天跟String打交道,对它再熟悉不过了.但是仔细看看,还是有一种拨开云雾的感觉, ...
- JS手机浏览器判断(转)
整理查询一下,js判断手机浏览器的方法 <script type="text/javascript"> /* * 智能机浏览器版本信息:包括微信内置 * */ var ...
- python中多线程与非线程的执行性能对比
此对比说明了一件事: 如果是IO型应用,多线程有优势, 如果是CPU计算型应用,多线程没必要,还有实现锁呢. #!/usr/bin/env python # -*- coding: utf-8 -*- ...
- python threading编程中的LOCK和RLOCK(可重入锁)
找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time sh ...
- LSM-Tree (BigTable 的理论模型)(转)
Google的BigTable架构在分布式结构化存储方面大名鼎鼎,其中的MergeDump模型在读写之间找到了一个较好的平衡点,很好的解决了web scale数据的读写问题. MergeDump的理论 ...
- Linux环境下使用perl编写CGI(httpd)
例子1: /var/www/cgi-bin/hello.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print ...
- 使用zookeeper实现分布式锁
简介: 核心是解决资源竞争的问题 分布式系统中经常需要协调多进程或者多台机器之间的同步问题,得益于zookeeper,实现了一个分布式的共享锁,方便在多台服务器之间竞争资源时,来协调各系统之间的协作和 ...
- codeforce ABBYY Cup 3.0 - Finals (online version) B2. Shave Beaver! 线段树
B2. Shave Beaver! The Smart Beaver has recently designed and built an innovative nanotechnologic a ...
- 配置tomcat下war包可以自压缩
<Host name="localhost" appBase="/home/hark/web" unpackWARs="true" a ...