牛客網-程式語言初學練習賽(第四場)題解
阿新 • • 發佈:2018-12-28
https://ac.nowcoder.com/acm/contest/312#question
只說可說的
D進位制A+B
十六進位制用%x,八進位制用%o
J競選社長
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <math.h> #include <algorithm> #define PI 3.1415926 using namespace std; int main() { int ch; int sum_a = 0, sum_b = 0; // 分別用來計數A和B的數量 // getchar()返回值是int型別,EOF是-1,代表檔案結束 // 這樣寫實現了一個字元一個字元的輸入字串,執行到檔案結束,換行時停止輸入 while ((ch = getchar()) != EOF && ch != '\n') { if (ch == 'O') break; else if (ch == 'A') sum_a++; else if (ch == 'B') sum_b++; } if (sum_a == sum_b) cout << "E" << endl; // 如果A的數量等於B的數量,輸出E else printf("%s\n", sum_a > sum_b ? "A" : "B"); // 條件運算子,如果A的數量大於B的數量,輸出A,否則輸出B return 0; }
2018.12.27 16:09 寢室