※1057 Number Zero One (20 points)

Given a string of strings with a length of no more than 10^​5, this question requires you to add up the serial numbers of all English letters (letters az correspond to serial numbers 1-26, regardless of case) to get the integer N, and then analyze it How many 0s and 1s are there in the binary representation of N. For example, given the string PAT (Basic), the sum of its alphabetical numbers is: 16+1+20+2+1+19+9+3=71, and the binary value of 71 is 1000111, that is, there are 3 0s and 4 1.

Input format:

Enter a character string that does not exceed 10^​5​​ in one line and ends with a carriage return.

Output format:

Output the number of 0 and the number of 1 in one line, separated by spaces. Note: If there is no letter in the string, it is considered that N does not exist, and there is no 0 and 1.

Input sample:

PAT (Basic)

Sample output:

3 4

answer:

#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>

using namespace std;

int main() {
    
    
	ios::sync_with_stdio(false);
	string s;
	getline(cin, s);
	transform(s.begin(), s.end(), s.begin(), ::tolower);//全部转换成小写--->头文件algorithm
	int sum = 0;
	for (char x : s) {
    
    
		if (isalpha(x)) sum += x - 96;
	}
	int zero = 0, one = 0;
	while (sum)
	{
    
    
		if (sum % 2 == 0) zero++;
		else one++;
		sum /= 2;
	}
	cout << zero << " " << one << endl;

	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44715943/article/details/115026506