其他分享
首页 > 其他分享> > PAT (Basic Level) Practice 1057 数零壹 (20 分)

PAT (Basic Level) Practice 1057 数零壹 (20 分)

作者:互联网

题目:1057 数零壹 (20 分)

来源:PAT (Basic Level) Practice

传送门 1057 数零壹

题面

image

题意:统计字符串中字母序号之和并转化为二进制,输出该二进制数的0和1的个数

思路:见代码

Code

点击查看代码
#include<iostream>
#include<string>

using namespace std;
typedef long long ll;

ll n[3];
int main() {
	string s;
	ll ans = 0;
	getline(cin, s);
	for (int i = 0; i < s.size(); i++) {
		s[i] = tolower(s[i]);
		if (s[i] <= 'z' && s[i] >= 'a') {
			ans += s[i] - 'a' + 1;
		}
	}
	while (ans) {
		n[ans % 2]++;
		ans /= 2;
	}
	cout << n[0] << " " << n[1];

	return 0;
}

标签:Basic,PAT,Level,ll,1057,ans,零壹
来源: https://www.cnblogs.com/w0x59-h/p/15860199.html