zoukankan      html  css  js  c++  java
  • 题目1.2.5 Ballon Comes

    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
     

     

    一. 题目核心:

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

    二. 总结知识点:

    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<<"
    ";
        }
    
    }
    技进乎艺,艺进乎道
  • 相关阅读:
    C#/Net代码精简优化技巧(二)【转】
    IsSpecialName特性及其他问题【转】
    &、|与&&、||的比较
    序列化后存储Cookie
    序列化反序列化操作
    C#/Net代码精简优化技巧(一)【转】
    .Net 中的序列化与反序列化【转】
    c#对象序列化(二进制序列化)BinaryFormatter【转】
    POJ 2828 Buy Tickets(单点更新) 详细题解和思路
    Color the ball HDU 1556 (非线段树做法)
  • 原文地址:https://www.cnblogs.com/weekend/p/4954917.html
Copyright © 2011-2022 走看看