HDOJ 1170 Balloon Comes!

【题意】:输入一个符号(加减乘除) 后边跟上两个操作数。输出结果,如果不是整数,保留两位小数。注意:The result should be rounded to 2 decimal places
If 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;
}
时间: 2024-12-17 04:20:15

HDOJ 1170 Balloon Comes!的相关文章

hdu 1170 Balloon Comes!

Balloon Comes! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 26455    Accepted Submission(s): 10055 Problem Description The contest starts now! How excited it is to see balloons floating aroun

hdoj 1004 Let the Balloon Rise(模拟 || 字典树)

Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84401    Accepted Submission(s): 31831 Problem Description Contest time again

HDOJ 1004 - Let the Balloon Rise(让气球飞起来)

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 138223    Accepted Submission(s): 54591 Problem Description Contest time again! How excited it is to see balloons floating ar

HDOJ 1004 Let the Balloon Rise (字符串+stl)

题目: Problem Description Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each co

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1

HDU2607 Let the Balloon Rise II

Let the Balloon Rise II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 131    Accepted Submission(s): 37 Problem Description Contest will be end at 17:00! How excited it is to see balloons flo

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

HDOJ-1004 Let the Balloon Rise

http://acm.hdu.edu.cn/showproblem.php?pid=1004 输入N个字符串  输出出现频率最高的字符串 # include <stdio.h> # include <string.h> # define MAX 1005 struct BALLOON { char Color[20]; int Times;//同颜色气球出现次数 }Balloon[MAX]; int n, Count; void Set_Balloon(char Color[])

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa