1057 數零壹
阿新 • • 發佈:2018-09-05
style div cin else ref getline out namespace str
題目鏈接:https://pintia.cn/problem-sets/994805260223102976/problems/994805270914383872
題解:
1 #include <iostream> 2 #include<string> 3 using namespace std; 4 5 int main() { 6 string str; 7 getline(cin, str); 8 int sum = 0, n_0 = 0, n_1 = 0; 9 for (int i = 0; i < str.size(); i++) {10 if ((str[i] >= ‘a‘&&str[i] <= ‘z‘) || (str[i] >= ‘A‘&&str[i] <= ‘Z‘)) { 11 if (str[i] >= ‘a‘&&str[i] <= ‘z‘) sum += (str[i] - ‘a‘ + 1); 12 if (str[i] >= ‘A‘&&str[i] <= ‘Z‘) sum += (str[i] - ‘A‘ + 1); 13 }14 } 15 while (sum) { 16 if (sum % 2 == 0) n_0++; 17 else n_1++; 18 sum /= 2; 19 } 20 cout << n_0 << " " << n_1; 21 return 0; 22 }
1057 數零壹