HDOJ 1170 Balloon Comes!

【题意】:输入一个符号(加减乘除) 后边跟上两个操作数。输出结果,如果不是整数,保留两位小数。注意:The result should be rounded to 2 decimal placesIf and only if it is not an integer.需要判断是不是整数,不是全部保留两位小数。

可以对整个字符串分割,或者采用一个char加两个int的输入。

【AC代码】:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;

#define MAX 30

int main()
{
    int T = 0;
    cin >> T;
    getchar();
    while (T--)
    {
        int i = 0, j = 0;
        char str[MAX], temp1[MAX], temp2[MAX];
        gets(str);
        for (i = 2, j = 0; str[i] != ' '; i++)
            temp1[j++] = str[i];
        temp1[j] = '\0';
        for (i++, j = 0; str[i] != ' ' && i < strlen(str); i++)
            temp2[j++] = str[i];
        temp2[j] = '\0';
        int a = atoi(temp1), b = atoi(temp2);
        switch(str[0])
        {
        case '+':
            cout << a + b << endl;
            break;
        case '-':
            cout << a - b << endl;
            break;
        case '*':
            cout << a * b << endl;
            break;
        case '/':
            if (a*1.0 / b != a/b)
                cout << fixed << setprecision(2) << a*1.0 / b << endl;
            else
                cout << fixed << setprecision(2) << a / b << endl;
            break;
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值