PAT1057 数零壹 (20 分)

题目

这题测试点有个不合理的地方:
没有英文字符的字符串。这个时候需要输出0 0,而不是1 0(25行)
在这里插入图片描述

代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main()
{
	//十进制
	char c;
	int total = 0;
	while (cin >> c)
	{
		if (c >= 'a'&&c <= 'z')
		{
			total += (int)(c - 'a') + 1;
		}
		else if (c >= 'A'&&c <= 'Z')
		{
			total += (int)(c - 'A') + 1;
		}
	}

	//二进制
	int num0 = 0;
	int num1 = 0;

	if (total == 0)num0 = 0;//如果没有任何英文 这是PAT的bug吧??难道0的个数不是1吗??
	else
	{
		while (total != 1 && total != 0)
		{
			if (total % 2 == 1)
			{
				num1++;
				total /= 2;
			}
			else if (total % 2 == 0)
			{
				num0++;
				total /= 2;
			}
		}
		num1++;
	}

	cout << num0 << ' ' << num1 << endl;

	system("pause");
}

猜你喜欢

转载自blog.csdn.net/sinat_42483341/article/details/88048913