给定一串长度不超过 105 的字符串,本题要求你将其中所有英文字母的序号(字母 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。
输入格式:
输入在一行中给出长度不超过 105、以回车结束的字符串。
输出格式:
在一行中先后输出 0 的个数和 1 的个数,其间以空格分隔。注意:若字符串中不存在字母,则视为 N 不存在,也就没有 0 和 1。
输入样例:
PAT (Basic)
输出样例:
3 4
#include<stdio.h>
#include<iostream>
using namespace std;
#include<string>
char str[100005] = { '0' };
int main()
{
int b, c;
int i = 0;
int d = 0;
int count = 0;
int remainder[100] = { 0 };
int collect[2] = { 0 };
int num1[28] = { 0 };
char num2[27] = { '0' };
num2[0] = 'a';
char ch = getchar();
while (ch != '\n') {
str[d] = ch;
ch = getchar();
d++;
}
for (int n = 0; n < 25; n++) {
num2[n + 1] = num2[n] + 1;
}
while (i<=d+1)
{
if (str[i] >= 'a'&&str[i] <= 'z') {
for (int j = 0; j < 26; j++) {
if (str[i] == num2[j]) {
num1[j + 1] += 1;
}
}
}
else {
if (str[i] >= 'A' && str[i] <= 'Z') {
for (int j = 0; j < 26; j++) {
if (str[i] == num2[j]-32) {
num1[j + 1] += 1;
}
}
}
}
i++;
}
for (int i = 1; i < 27; i++) {
for (int j = 0; j < num1[i]; j++) {
count += i;
}
}
int k = 0;
while (count != 0)
{
b = count / 2;
k++;
remainder[k] = count - b * 2;
count = count / 2;
}
for (int i = 1; i < k + 1; i++) {
c = remainder[i];
collect[c] += 1;
}
if (collect[0] == 0 && collect[1] == 0) {
printf("%d %d\n", 0, 0);
}
else {
printf("%d %d\n", collect[0], collect[1]);
}
return 0;
}
数组给大点就能过测试点3,4.