题目1 : Arithmetic Expression

it2022-05-08  23

时间限制: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.

样例输入4901 / 1003 * 32 + 68 - -1样例输出2

#include <iostream> #include <cmath> #include <cfloat> using namespace std; int main(){int N; cin >> N; int index = 1; double res = DBL_MAX; double ans; for(int i = 1 ; i <= N; ++i){ double a,b; char op; cin >> a >> op >> b; switch(op){ case '+': ans = fabs(a + b - 9); break; case '-': ans = fabs(a - b - 9); break; case '*': ans = fabs(a * b - 9); break; default: ans = fabs(a / b - 9); } if(ans < res) { index = i; res = ans; } } cout << index << endl; return 0; }

 

转载于:https://www.cnblogs.com/redhead/p/3650434.html

相关资源:arithmetic expression evaluation

最新回复(0)