您的位置:首页 > 其它

【ACM】hdu_1170_Balloon Comes!_201307261946

2013-07-26 20:22 417 查看
Balloon Comes!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16847 Accepted Submission(s): 6150

Problem Description
The contest starts now! How excited it is to see balloons floating around. You, one of the best programmers in HDU, can get a very beautiful balloon if only you have solved the very very very... easy problem.
Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.
Is it very easy?
Come on, guy! PLMM will send you a beautiful Balloon right now!
Good Luck!

Input
Input contains multiple test cases. The first line of the input is a single integer T (0<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator.

Output
For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.

Sample Input
4
+ 1 2
- 1 2
* 1 2
/ 1 2

Sample Output
3
-1
2
0.50

#include <stdio.h>
int main()
{int n;
scanf("%d",&n);
getchar();
while(n--)
{int a,b;
char c;
scanf("%c%d%d",&c,&a,&b);
getchar();
if(c=='/')
{if(a/b==(double)a/b)
printf("%d\n",a/b);
else
printf("%.2lf\n",(double)a/b);
}
if(c=='+')
printf("%d\n",a+b);
if(c=='-')
printf("%d\n",a-b);
if(c=='*')
printf("%d\n",a*b);
}
return 0;
}

第一次少用了第二个getchar();而且没有考虑a/b结果为整数应输出整数
注意:要用两次getchar(); 当且仅当a/b结果为小数时,结果保留两位小数,结果不为小数时,输出整数

读入字符要注意两点。第一:注意空格,例如输入a b那么要写成scanf("%c %c",&x,&y),这样可以把空格中间的空格读掉,因为空格也是一个字符,如果不这样做的话字符b就读不到;第二,读完字符后要加一个getchar(),因为scanf是不能把回车换行符读取的,故当读入第二组数据的时候读的是上一组数据留下来的换行符这个字符。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: