F - Balloon Comes!

    技术2022-07-16  89

    F - Balloon Comes!

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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

    题意分析:

    这道题就是有T组输入,每组输入包括一个操作符和两个操作数。根据操作符对两个操作数做相应的加减乘除运算。

    解题思路:

    这道题本身就判断符号,进行相应的运算就可以了,需要注意的一点就是对于如果结果是整数的情况,要按int类型输出,如果结果是小数,要控制小数点后两位。因为在加减乘中因为输入类型为int,所以直接进行整数的运算就好了,对于除法先要进行强制数据类型转换,然后在运算。

    编码:

    #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { char op; int a, b; cin >> op >> a >> b; if (op == '+') cout << a + b << endl; else if (op == '-') cout << a - b << endl; else if (op == '*') cout << a * b << endl; else if (op == '/') { double ans = (double)a / (double)b; if (a % b == 0) cout << a / b << endl; else { printf("%.2f\n", (float) a / b); } } } return 0; }

    最后:

    通过这道题的做题过程,最大的问题就在于最后这条语句:printf("%.2f\n", (float) a / b);上,之前一直用cout,再加控制格式,但是交了两发都是Wrong Answer。另外,这道题控制了操作数的大小范围,所以在运算本身上没有难度。有关这个地方printf和cout的区别还没有搞清楚,另外还有一个疑问,举例: / 1555 1000 应该输出1.56,但是结果是1.55 但是仍能AC,虽然可以知道1.555本身可能在计算机中存储的是1.5549999......

     

    Processed: 0.011, SQL: 9