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 integersai (≤
- 1) follow
Output
Output one line,including an integer representing the number of 'a' in the group of given numbers.
Sample
Input | Output |
---|---|
3 97 24929 100 | 3 |
Translate:
输入包含一组测试数据。第一个数字是一个正整数 N (1≤N≤100),然后是 N 个正整数 (1≤ai≤2^32 - 1 )
输出一行,包括一个整数,表示给定数字组中“a”的数量。
内心: 这不简单????只要是一个数大于等于97不就是包含了一个‘a’了?看看样例,的确如此!
........Wrong Answer.......
这道题考察的非常之精细
思路:
题目给出一个字符由8位二进制数字表示,那么我们每次就看8位,如果得到的数为97sum++;
接着把这8位移走。
代码:
#include<iostream>
#include<math.h>
using namespace std;
int sum;
int main()
{
int n;
cin >> n;
while (n--)
{
int a;
int t = pow(2, 8);
cin >> a;
while (a)
{
if (a%t==97)sum++;
a >>=8;
}
}
cout << sum << endl;
}