HDU 5980 - Find Small A(进制)

本文介绍了一个编程问题,任务是计算一组32位整数中字符'a'出现的次数。通过将整数转换为二进制并检查每8位是否等于字符'a'的ASCII值97来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Find Small A

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1680    Accepted Submission(s): 826


Problem Description
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≤  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
 

Source
 

给你n个十进制的数,让你把它变成2进制,然后每8位算一下是不是等于97,是就ans++。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <math.h>
#include <vector>
using namespace std;
const int maxn = 55;
int a[maxn];
int ans;
int num=0;
void er(int x)
{
    num=0;
    while(x){
        num++;
        a[num]=x%2;
        x=x/2;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        ans=0;
        for(int i=1;i<=n;i++){
            int p;scanf("%d",&p);
            er(p);
            int cnt=0;
            int now=0;
            for(int j=1;j<=num;j++){
                cnt++;
                if(a[j]){
                    now+=1<<(cnt-1);
                }
                if(cnt==8){
                    if(now==97){
                        ans++;
                    }
                    now=0;
                    cnt=0;
                }
            }
            if(now==97){
                ans++;
            }
        }
        printf("%d\n",ans);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值