PAT乙级——1057. 数零壹(20)-native

    xiaoxiao2022-08-06  117

    思路:首先我们要把如果有英文字母的大写转换成小写来求出字符串的所代表数字,然后用除二取整法记录每一次0的个数和1的个数。

    #include<cstdio> #include<cstring> #include<cctype> int main(){ char c[1000005]; gets(c); int len=strlen(c); int N=0; for(int i=0;i<len;i++) if(tolower(c[i])>='a'&&tolower(c[i])<='z')//tolower()要包含头文件<cctype> N+=(tolower(c[i])-'a'+1);//既然a代表1,我们要在计算的时候再加1 int count0=0,count1=0; //count0记录0的个数,count1记录1的个数 while(N){ if(N%2) count1++; else count0++; N=N/2; } printf("%d %d",count0,count1); return 0; }

    题目链接:

    https://www.patest.cn/contests/pat-b-practise/1057

    转载请注明原文地址: https://ju.6miu.com/read-1132095.html
    最新回复(0)