1. 程式人生 > >hihocoder Arithmetic Expression【在線查詢】

hihocoder Arithmetic Expression【在線查詢】

limit 時間 cout main ins font multipl exp 限制

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.

樣例輸入
4
901 / 100
3 * 3
2 + 6
8 - -1
樣例輸出
2

【題意】:算出的結果最接近9的為第幾個,相同的話輸出靠前的。註意卡精度!要用double~
【代碼】:
技術分享圖片
#include <bits/stdc++.h>
#define LL long long
#define
maxn 500005 const int inf = 0x3f3f3f3f; using namespace std; int n,idx; double a,b,sum=0; char op; double mi=0x3f3f3f3f3f; void cal() { switch(op) { case +: sum=a+b; break; case -: sum=a-b; break; case *: sum=a*b;
break; case /: sum=a/b; break; } } int main() { cin>>n; for(int i=1;i<=n;i++) { cin>>a>>op>>b; cal(); double now = abs(9-sum); if(now < mi)//在線查詢,邊輸入邊查詢,也可以叫打擂臺算法,誰小誰上當min King,並記錄是第幾個人 { idx=i;//而且是now < mi 不能等於,無形記錄了字典序最小的那個人,因為後面出現的也是相等,不是小於了! mi=now; } } cout<<idx<<endl; }
打擂臺 嚴格小於


hihocoder Arithmetic Expression【在線查詢】