时间限制: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<string>
#include<stdlib.h>
#include<math.h>
using namespace std; double caluculate(double a, double b, char op)
{
if(op == '+')
{
return a + b;
}
else if(op == '-')
{
return a - b;
}
else if(op == '*')
{
return (double)(a * b);
}
else if(op == '/')
{
return (double)(a / b);
}
} int main(void)
{
int amount = ;
cin>>amount;
double tmpmin = ;
int position = -;
for(int i=; i<amount; ++i)
{
double a,b;
char op;
cin>>a;
cin>>op;
cin>>b;
double tmpresult = caluculate(a,b,op);
double tmp_result = fabs(-tmpresult);
if(tmp_result < tmpmin)
{
tmpmin = tmp_result;
position = i+;
}
} cout<<position<<endl; return ;
}

遇到的问题:

  • 一开始没有发现结果应该是double的,int型的话会导致除法的小数位看不到
  • 刚开始使用了stdlib.h中的abs()函数,后来发现应该用math.h中的fabs(),看来自己对math.h中的许多函数都不是很熟悉,不能熟练应用,随后补充

Arithmetic Expression的更多相关文章

  1. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  2. 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 ...

  3. hihocoder Arithmetic Expression【在线查询】

    Arithmetic Expression   时间限制:2000ms 单点时限:200ms 内存限制:256MB 描述 Given N arithmetic expressions, can you ...

  4. ACM Arithmetic Expression

    Description Given N arithmetic expressions, can you tell whose result is closest to 9? Input Line 1: ...

  5. 【微软编程一小时】题目1 : Arithmetic Expression

    时间限制:2000ms 单点时限:200ms 内存限制:256MB 描写叙述 Given N arithmetic expressions, can you tell whose result is ...

  6. Variables and Arithmetic Expression

    Notes from The C Programming Language A decimal point in a constant indicates that it is floating po ...

  7. 【Codeforces 115D】Unambiguous Arithmetic Expression

    Codeforces 115 D 题意:给一个没有括号的表达式,问有多少种添加括号的方法使得这是一个合法的表达式?输入可能有正负号.加减乘除.数字. 思路1: 这是不能过的\(naive\)的\(dp ...

  8. [Erlang 0118] Erlang 杂记 V

       我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下.    做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...

  9. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. JavaScript要点 (一) 变量-作用域

    JavaScript 作用域 作用域—可访问变量的集合. 全局变量或者函数可以覆盖window对象的变量或者函数: 局部变量和window对象可以覆盖全局变量和函数. JavaScript 作用域 在 ...

  2. iOS 9 学习系列:UIStack View (转载)

    作者:Nathan_Bao 地址:http://www.jianshu.com/p/1991e6c2881a 在 iOS9 中,Apple 引入了 UIStackView,他让你的应用可以通过简单的方 ...

  3. keystone之预备知识点

    1.webobwebob是一个用来封装http request和http response的一个库,都封装成实例,方便解析http request和构建http response.最佳教程地址: ht ...

  4. PHP函数ip2long转换IP时数值太大产生负数的解决办法

    有两种办法: 1. bindec( decbin($long))  利用bindec和decbin两个函数转换一次就没有问题了 我一直在用上面的方法,但是在升级到PHP7以后就不起作用了(因为最近只进 ...

  5. uva 624 CD 01背包打印路径

    // 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #includ ...

  6. WaterWave

    WaterWave.rar

  7. [MODx] Build a CMP (Custom manager page) using MIGX in MODX 2.3 -- 2

    We are not finishing yet... 1. Under MIGX Management, we need to add some "Actionbuttons" ...

  8. Mysql shell 控制台---mysqlsh

    原创 2016-07-12 杜亦舒 性能与架构 以前登录Mysql的控制台后,使用SQL语言来操作数据库,如 mysql> select * from tablename; Mysql 5.7. ...

  9. MySQL(6):数据操作

    1.创建数据(插入数据) (1)insert into  tab_name(字段列表)  values(值列表) (2)如果需要在插入时,为所有的字段设置值,那么可以省略字段列表.要求是值的顺序,应该 ...

  10. Android 自学之绝对布局 AbsoluteLayout

    绝对布局(AbsoluteLayout),绝对布局就像java AWT中的空布局:所谓的绝对布局就是Android不提供任何的布局控制,而是有开发人员自己通过X坐标和Y坐标来控制组件的位置.当使用绝对 ...