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;}