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

    1010: Triangles Time Limit: 2 Sec   Memory Limit: 128 MB Submit: 18   Solved: 8 Description You are ...

  2. listview自定义背景以及item自定义背景

    item向自定义背景,可以根据position来设置不同的背景. listview背景设置是需要注意设置下面这几项: //点下时整个页面的背景 android:cacheColorHint=" ...

  3. 数据库升级ora-04063 DBMS_REGISTRY has error

    在做Oracle数据库从11.2.0.1.0升级到11.2.0.2.8时,软件升级没有问题,实例升级没有问题,升级psu的时候. 执行@?/rdbms/admin/catbundle psu appl ...

  4. android自动化测试中hierarchyviewer和uiautomatorviewer获取控件信息的方式比对

    http://blog.csdn.net/itfootball/article/details/21777835 http://blog.csdn.net/chenbang110/article/de ...

  5. Tomcat 生产服务器性能优化

    虑一下这种场景,你开发了一个应用,它有十分优秀的布局设计,最新的特性以及其它的优秀特点.但是在性能这方面欠缺,不管这个应用如何都会遭到客户拒绝.客户总是期望它们的应用应该有更好的性能.如果你在产品中使 ...

  6. iOS开发——面试笔试精华(一)

    面试笔试精华(一) 1.        #import 跟#include.@class有什么区别?#import<> 跟 #import”"又什么区别? 1>  #imp ...

  7. careercup-C和C++ 13.4

    13.4 深拷贝和浅拷贝有什么区别,如何使用? 解答 浅拷贝并不复制数据,只复制指向数据的指针,因此是多个指针指向同一份数据. 深拷贝会复制原始数据,每个指针指向一份独立的数据.通过下面的代码, 可以 ...

  8. CSS_样式sample

    <!DOCTYPE HTML> <html> <head> <title>div浮动</title> <style type=&quo ...

  9. eclipse安装android sdk后工具栏没有图标的设置

    如果没有出现这android图标,选择'Window>Customize Perspective...>Commands',并在'Available command groups'中勾选' ...

  10. NSURLSession 请求

    参考网站:http://ningandjiao.iteye.com/blog/2010753 http://www.cocoachina.com/industry/20131106/7304.html ...