zoukankan      html  css  js  c++  java
  • 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,0);
        for(int i = 0 ; i < n; ++ i){
            double a,b;
            char op;
            cin >> a >> op >> b;
            switch(op){
            case '+':
                res[i] = fabs(a+b-9);
                break;
            case '-':
                res[i] = fabs(a-b-9);
                break;
            case '*':
                res[i] = fabs(a*b-9);
                break;
            case '/':
                res[i] = fabs(a/b-9);
                break;
            default:
                break;
            }
        }
        cout<<distance(res.begin(), min_element(res.begin(),res.end()))+1<<endl;
        return 0;
    }
  • 相关阅读:
    缓存读写策略
    支撑京东小程序的开发框架 「Taro」
    Zookeeper vs Etcd
    前端开发利器 Web Replay
    kafka 中 zookeeper 具体是做什么的?
    newSQL 到底是什么?
    zookeeper配置集群
    zookeeper配置文件说明
    ssh远程访问-提示密钥不安全
    Nodejs-log4js使用配置
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3647495.html
Copyright © 2011-2022 走看看