【算法笔记】B1057 数零壹
作者:互联网
思路:
步骤1:把大写字母改成小写字母;
步骤2:把每个字母的序号加到sum里得到序号和;
步骤3:序号和换算成二进制,保存到数组里,统计0和1的个数。
code
1 #include<bits/stdc++.h> 2 using namespace std; 3 vector<int> binary; 4 int main(){ 5 int sum = 0; 6 string str; 7 getline(cin, str); 8 int len = str.length(); 9 for(int i = 0; i < len; i++){ 10 if(str[i]>='A'&&str[i]<='Z') str[i] += 32; 11 if(str[i]>='a'&&str[i]<='z') sum += (str[i] - 'a' + 1); 12 } 13 while(sum){ 14 binary.push_back(sum % 2); 15 sum /= 2; 16 } 17 int zero = 0, one = 0; 18 for(int i = 0; i < binary.size(); i++){ 19 if(binary[i] == 0) zero++; 20 else if(binary[i] == 1) one++; 21 } 22 cout<<zero<<" "<<one; 23 return 0; 24 }
标签:int,步骤,sum,B1057,len,算法,str,序号,零壹 来源: https://www.cnblogs.com/chunlinn/p/10811263.html