HDOJ 1170 Balloon Comes!,hdojballoon

來源:互聯網
上載者:User

HDOJ 1170 Balloon Comes!,hdojballoon

【題意】:輸入一個符號(加減乘除) 後邊跟上兩個運算元。輸出結果,如果不是整數,保留兩位小數。注意: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 30int 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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.