PTA 乙级——1057 数零壹 C++实现
发布时间
阅读量:
阅读量
题目 数零壹
给定一串长度不超过 10^5的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0、多少 1。例如给定字符串 PAT (Basic),其字母序号之和为:16+1+20+2+1+19+9+3=71,而 71 的二进制是 1000111,即有 3 个 0、4 个 1。
输入格式:
输入在一行中给出长度不超过 10^5、以回车结束的字符串。
输出格式:
在一行中先后输出 0 的个数和 1 的个数,其间以空格分隔。
输入样例:
PAT (Basic)
输出样例:
3 4
代码
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
int total = 0;
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= 'a'&&s[i] <= 'z')
total = total + s[i] - 'a' + 1;
else if (s[i] >= 'A'&&s[i] <= 'Z')
total = total + s[i] - 'A' + 1;
}
int zero = 0, one = 0;
int i = 2;
while (true)
{
if (total == 0)
break;
if (total%i == 0)
zero++;
else
{
total -= i / 2;
one++;
}
i *= 2;
}
cout << zero << " " << one;
}
全部评论 (0)
还没有任何评论哟~
