1057 数零壹 (20分)
https://pintia.cn/problem-sets/994805260223102976/problems/994805270914383872
第一次:段错误
错误代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <cmath> 5 #include <algorithm> 6 #include <iostream> 7 using namespace std; 8 int main() 9 { 10 int arr[10]={0},arr1[3]={0}; 11 int i=0,j=0,k=0,sum=0; 12 string str; 13 getline(cin,str); 14 for(i=0;i<str.length();i++) 15 { 16 if(str[i]>=‘a‘&&str[i]<=‘z‘) 17 { 18 arr[k]=str[i]-‘a‘+1; 19 k++; 20 } 21 else if(str[i]>=‘A‘&&str[i]<=‘Z‘) 22 { 23 arr[k]=str[i]-‘A‘+1; 24 k++; 25 } 26 } 27 for(i=0;i<k;i++) sum+=arr[i]; 28 while(sum>0) 29 { 30 int temp=sum%2; 31 arr1[temp]++; 32 sum/=2; 33 } 34 cout<<arr1[0]<<" "<<arr1[1]<<endl; 35 return 0; 36 }
第二次:AC
正确代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <cmath> 5 #include <algorithm> 6 #include <iostream> 7 using namespace std; 8 int main() 9 { 10 int sum=0; 11 string str; 12 getline(cin,str); 13 for(int i=0;i<str.length();i++) 14 { 15 if(str[i]>=‘a‘&&str[i]<=‘z‘) sum+=str[i]-‘a‘+1; 16 else if(str[i]>=‘A‘&&str[i]<=‘Z‘) sum+=str[i]-‘A‘+1; 17 } 18 int count0=0,count1=0; 19 while(sum>0) 20 { 21 int temp=sum%2; 22 if(temp==0) count0++; 23 else count1++; 24 sum/=2; 25 } 26 cout<<count0<<" "<<count1<<endl; 27 return 0; 28 }
原文地址:https://www.cnblogs.com/jianqiao123/p/12227566.html
时间: 2024-11-05 18:58:34