題目描述
Problem Description
衆所周知,字元’a’的ASCII碼是97。
現在,求一組給定的數字中有多少個字母a。
請注意,這裡的數字是由計算機中的32位整數給出的。
也就是說,1位代表4個字元(一個字元由8位二進制數字表示)。
Input
多組輸入,每組輸入第一行是n,表示有n個整數,接下來給出n個數a(i)。
(n<=100,1<=a(i)<2^32)
Output
輸出有多少個字元a
Sample Input
3
97 24929 100
Sample Output
3
題目分析
題目說的有點繞,但是題目的本質已經在标題裡面展現出來了。我們隻需要将32位十進制整數轉為8位二進制整數對每一位進行判斷即可。是以也無需轉換完判斷,循環轉制拆位取模判斷即可
AC Code
#include <bits/stdc++.h>
#define
using namespace std;
int n, cnt, x;
int main(){
IOF;
while (cin >> n){
cnt = 0;
for (int i = 0; i < n; i++){
cin >> x;
int tmp;
tmp = x % 256; if(tmp == 97) cnt++;
tmp = x / 256 % 256; if(tmp == 97) cnt++;
tmp= x / (256 * 256) % 256; if(tmp == 97) cnt++;
tmp = x / (256 * 256 * 256); if(tmp == 97) cnt++;
}
cout << cnt << endl;
}
}