码迷,mamicode.com
首页 > 其他好文 > 详细

PTA乙级 (1057 数零壹 (20分))

时间:2020-01-22 01:17:53      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:ima   class   algo   ems   image   alt   color   while   include   

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 }

PTA乙级 (1057 数零壹 (20分))

标签:ima   class   algo   ems   image   alt   color   while   include   

原文地址:https://www.cnblogs.com/jianqiao123/p/12227566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!