F-进制转换

题目描述

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;
    }
}

猜你喜欢

转载自blog.csdn.net/yanweiqi1754989931/article/details/113745228