2014微软编程一小时题目1 : Arithmetic Expression
2014-04-16 11:01
357 查看
Arithmetic Expression
时间限制: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.
样例输入
样例输出
时间限制: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
#include <iostream> #include <vector> #include <stdlib.h> #include <math.h> using namespace std; int main() { //input n int n; cin >> n; if(n < 1 || n > 50000) return -1; //input a op b vector<double> v; int a ,b = 0; char op; double distance = 10000000;//表示和9的距离 while(n-- > 0 && cin >> a >> op >> b ) { switch (op) { case '+' : v.push_back(fabs((double)(a + b - 9))); case '-' : v.push_back(fabs((double)(a - b - 9))); case '*' : v.push_back(fabs((double)(a * b - 9))); case '/' : v.push_back(fabs((double)a / (double)b - 9)); } } //比较distance谁小 double small = v[0]; vector<double>::difference_type number = 0; for(vector<double>::iterator iter = v.begin() + 1 ; iter != v.end(); ++iter) { if((*iter) < small) { small = *iter; number = iter - v.begin(); } } cout << number +1 << endl; //system("pause");//在GCC中编译这个程序的时候一定要加上stdlib.h这个头文件才能编译这一句 return 0; }
相关文章推荐
- 微软编程一小时活动 - 暨微软2014实习生招募编程模拟测试 题目1 : Arithmetic Expression 原创解答
- 2014微软编程一小时 题目1 : Arithmetic Expression Java实现
- 2014微软编程一小时题目2 : Longest Repeated Sequence
- 【微软编程一小时】题目1 : Arithmetic Expression
- 微软编程一小时比赛--题目1 : Arithmetic Expression
- 微软编程一小时题目一
- 微软2014编程之美初赛第一场——题目2 : 树
- 微软2014编程之美初赛第一场——题目2 : 树
- 微软2014编程之美挑战赛初赛第二场 题目2 : 字符串压缩
- 微软编程一小时题目二
- 微软编程一小时题目1 : Arithmetic Expression
- Longest Repeated Sequence【微软编程一小时-题目2】
- 微软2014编程之美初赛第一场——题目3 : 活动中心
- 微软2014实习生及校招秋令营技术类职位,在线编程题目
- 算法 Longest Repeated Sequence(最大重复子序列 微软编程一小时 题目2)
- 微软2014编程之美初赛第一场——题目1 : 焦距
- 微软2014实习生及校招秋令营技术类职位,在线编程题目及解答。
- 微软编程一小时 题目2: Longest Repeated Sequence
- 微软2014在线编程一小时活动第一题
- 微软2014编程之美初赛第一场——题目3 : 活动中心