題目描述
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位二進制整數對每一位進行判斷即可,因此也無需轉換完判斷,回圈轉制拆位取模判斷即可

不難發現,32位二進制整數拆出四個字符來,回圈也沒必要了,手寫一下程序然后復制粘貼幾次就完成了,
AC Code
#include <bits/stdc++.h>
#define IOF ios_base::sync_with_stdio(false)
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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/258178.html
標籤:其他
