c题解 1057 数零壹 (20 分)
原题
https://pintia.cn/problem-sets/994805260223102976/problems/994805270914383872
代码
#include
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<
贴一下别人的代码。
学到三个函数int isalpha(int _C)
判断字符是否为英文字母,int toupper(int _C)
把字符转换为大写,
getline(cin, str)
读取一行。(cin只能读到空格前)。
#include
#include
#include
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;
}