题面
As is known to all,the ASCII of character 'a' is 97. Now,find out how many character 'a' in a group of given numbers. Please note that the numbers here are given by 32 bits’ integers in the computer. That means,1digit represents 4 characters(one character is represented by 8 bits’ binary digits).
Input
The input contains a set of test data. The first number is one positive integer N (1≤N≤100),and then N positive integers (ai(1≤ ai≤2^{32} - 1)) follow
Output
Output one line,including an integer representing the number of 'a' in the group of given numbers.
Sample Input
3
97 24929 100
Sample Output
3
题目分析
题意:
众所周知,在ASCII码中,a
的值为97。现在给你一组整数,请你计算里面有多少个a
。
请注意,所给的数字在计算机中都是32位整数,而一个字符占8位
解题思路:
本题只需要判断每一个数字(x),然后“8位8位”的看——每次只看二进制中的后八位(也就是 x % (2^8) ),检查这8位中是否有一个a
,即x % 256 == 97
。当前后8位判完后,再右移8位(即除以(2^8)),判断下一组8位,直到x == 0
。
AC代码
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
__int32 x;
while (cin >> n) {
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> x;
while (x) {
if (x % 256 == 'a')
ans++;
x >>= 8;
}
}
cout << ans << endl;
}
return 0;
}