您的位置:首页 > 其它

题目1.2.5 Ballon Comes

2015-11-10 23:16 197 查看
[align=left]Problem Description[/align]
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!

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

4
+ 1 2
- 1 2
* 1 2
/ 1 2


[align=left]Sample Output[/align]

3
-1
2
0.50


一. 题目核心:

  除法运算时,输出结果为小数时,对输出结果的处理。

二. 总结知识点:

1.对cout输出结果小数点位数的控制。

先声明头文件#include<iomanip>

cout.precision(n);//n为输出结果小数点右边保留的位数

cout.setf(ios::fixed);

cout<<Result;

2.掌握两个运算符

%-->取余运算,可用来判断一个数是否可以整除另一个数。

/-->取模运算,如果结果需要保留小数,除数和被除数需要有一个为float或者double类型。

3.实现N个案例的输入输出。

先输入一个正整数N,表示测试案例的个数

然后通过判断语句去判断输入的测试案例数目是否达到N,若未达到,继续输入测试案例,否则退出程序。

三. 题目代码:

#include <iostream>
#include <iomanip>
#include "math.h"
using namespace std;
int main()
{
int T,A,B;
char C;
int i=0;
cin>>T;
while(i != T) {
cin>>C;
cin>>A>>B;

switch (C) {
case '+':
cout<<A+B;
break;
case '-':
cout<<A-B;
break;
case '*':
cout<<A*B;
break;
case '/':
if(A%B!=0)
{
cout.precision(2);
cout.setf(ios::fixed);
cout<<float(A)/B;
}
else
cout<<A/B;
break;
default:
break;
}
i++;
cout<<"\n";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: