时间限制: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. FormsAuthentication.GetRedirectUrl 方法

    https://msdn.microsoft.com/zh-cn/library/8a22t5t3(v=vs.80) FormsAuthentication.GetRedirectUrl 方法 .NE ...

  2. spring jdbcTemplate源码剖析

    本文浅析 spring jdbcTemplate 源码,主要是学习其设计精髓.模板模式.巧妙的回调 一.jdbcTemplate 类结构 ①.JdbcOperations : 接口定义了方法,如 &l ...

  3. ASP终极防下载(转)

    自从搞ASP+ACCESS没少为避免数据库下载而伤过神,网上的奇淫技巧更是数不胜数,本文就是同大家共同探讨各路前辈的留下的秘笈并指中其中的优劣,最后为大家提供一种最佳的解决方案. 一.开篇 自从搞AS ...

  4. 本地win8系统部署网站遇到的问题

    网站开发环境:vs2013 .操作系统是win8.1系统. iis8   win8系统激活码:00261-30000-00000-AA825 需要部署的网站文件夹放在了桌面上,在iis里配置时,无法启 ...

  5. c# label的内容显示不全

    c# label的内容显示不全.须要设置例如以下属性就可以: 1.将Lable的font属性的字体改成宋体: 2.将AutoSize属性改成true: 然后内容就能够显示所有了.

  6. Oracle Hints具体解释

    在向大家具体介绍Oracle Hints之前,首先让大家了解下Oracle Hints是什么,然后全面介绍Oracle Hints,希望对大家实用.基于代价的优化器是非常聪明的,在绝大多数情况下它会选 ...

  7. 操作BLOB、CLOB、BFILE

    BFILE        二进制文件,存储在数据库外的操作系统文件,只读的.把此文件当二进制处理. BLOB        二进制大对象.存储在数据库里的大对象,一般是图像声音等文件. CLOB    ...

  8. starling localToGlobal

    o.localToGlobal(new Point());   返回o在舞台的全局坐标... o.parent.localToGlobal(new Point(o.x,o.y))   //返回指定坐标 ...

  9. careercup-C和C++

    13.1 用C++写个方法,打印输出文件的最后K行. 解答: 一种方法是打开文件两次,第一次计算文件的行数N,第二次打开文件,跳过N-K行, 然后开始输出.如果文件很大,这种方法的时间开销会非常大. ...

  10. mac中vmware tools进行磁盘压缩和vmware-tools-cli的使用方法

    前言: 高高兴兴的在vmware9.0中安装了mac10.8系统,然后学习iphone开发,但是发现下载的pdf都是基于xcode3.2.5的,又在10.8上面安装3.2.5,出现“五国”无法解决,最 ...