c题解 1057 数零壹 (20 分)(C problem solving 1057 zero one (20 points))

原题

https://pintia.cn/problem-sets/994805260223102976/problems/994805270914383872

代码

#include <iostream>
using namespace std;

int main()
{
    char c;
    int total = 0;

    while (c != '\n')
    {
        scanf("%c", &c);
        if (c >= 'A' && c <= 'Z')
        {
            c = c + ('a' - 'A');
        }
        if (c >= 'a' && c <= 'z')
        {
            total += c - 'a' + 1;
        }
    }

    int one=0,zero=0;
    while (total>0)
    {
        total%2==1?one++:zero++;
        total=total/2;
    }

    // if(one>0||zero>0)  
    cout<<zero<<" "<<one;
    return 0;
}

贴一下别人的代码。
学到三个函数判断字符是否为英文字母,把字符转换为大写,
读取一行。(cin只能读到空格前)。

int isalpha(int _C)
int toupper(int _C)
getline(cin, str)
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main() {
    string s;
    getline(cin, s);
    int n = 0;
    for(int i = 0; i < s.length(); i++) {
        if(isalpha(s[i])) {
            s[i] = toupper(s[i]);
            n += (s[i] - 'A' + 1);
        }
    }
    int cnt0 = 0, cnt1 = 0;
    while(n != 0) {
        if(n % 2 == 0) {
            cnt0++;
        } else {
            cnt1++;
        }
        n = n / 2;
    }
    printf("%d %d", cnt0, cnt1);
    return 0;
}
————————

Original title

https://pintia.cn/problem-sets/994805260223102976/problems/994805270914383872

code

#include <iostream>
using namespace std;

int main()
{
    char c;
    int total = 0;

    while (c != '\n')
    {
        scanf("%c", &c);
        if (c >= 'A' && c <= 'Z')
        {
            c = c + ('a' - 'A');
        }
        if (c >= 'a' && c <= 'z')
        {
            total += c - 'a' + 1;
        }
    }

    int one=0,zero=0;
    while (total>0)
    {
        total%2==1?one++:zero++;
        total=total/2;
    }

    // if(one>0||zero>0)  
    cout<<zero<<" "<<one;
    return 0;
}

Post someone else’s code.
Learn three functions to judge whether characters are English letters and convert characters to uppercase,
Read one line. (CIN can only read before the space).

int isalpha(int _C)
int toupper(int _C)
getline(cin, str)
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main() {
    string s;
    getline(cin, s);
    int n = 0;
    for(int i = 0; i < s.length(); i++) {
        if(isalpha(s[i])) {
            s[i] = toupper(s[i]);
            n += (s[i] - 'A' + 1);
        }
    }
    int cnt0 = 0, cnt1 = 0;
    while(n != 0) {
        if(n % 2 == 0) {
            cnt0++;
        } else {
            cnt1++;
        }
        n = n / 2;
    }
    printf("%d %d", cnt0, cnt1);
    return 0;
}