HDU Find Small A(进制转换 位运算)

本文介绍了一种算法,用于在一组32位整数中查找字符'a'的ASCII表示,并统计其出现次数。通过位操作实现高效查找。

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

Find Small A

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


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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   6018  6017  6016  6015  6014 


首先asc2码是8bits的,所以我们要以8bits为一个单位在给出的数字中找‘a’,用了两种方法,一种是进制转换成二进制来做,这样显然太麻烦了,按照位运算来做就简单多了
////
////  main.cpp
////  Find Small A
////
////  Created by 张嘉韬 on 2017/3/19.
////  Copyright © 2017年 张嘉韬. All rights reserved.
////
//
//#include <iostream>
//#include <cstring>
//#include <cstdio>
//using namespace std;
//typedef long long ll;
//const int MAXN=32+10;
//const char OR[9]="10000110";
//char a[MAXN];
//int len;
//void tran(ll temp)
//{
//    len=0;
//    for(int i=0;i<=MAXN;i++) a[i]='0';
//    while(temp!=0)
//    {
//        a[len++]=temp%2+'0';
//        temp=temp/2;
//    }
//}
//ll match()
//{
//    ll cnt=0;
//    for(int i=0;i<len;i+=8)
//    {
//        int flag=1;
//        for(int j=0;j<8;j++)
//        {
//            if(OR[j]!=a[i+j])
//            {
//                flag=0;
//                break;
//            }
//        }
//        if(flag) cnt++;
//    }
//    return cnt;
//}
//ll match(ll temp)
//{
//    ll cnt=0;
//    
//    return cnt;
//}
//int main(int argc, const char * argv[]) {
//    //freopen("/Users/zhangjiatao/Desktop/input.txt","r",stdin);
//    int n;
//    while(scanf("%d",&n)!=EOF)
//    {
//        ll counter=0;
//        for(int i=1;i<=n;i++)
//        {
//            ll temp;
//            scanf("%lld",&temp);
//            tran(temp);
//            ll cnt=match();
//            counter+=cnt;
//        }
//        printf("%lld\n",counter);
//    }
//    return 0;
//}



#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
    //freopen("/Users/zhangjiatao/Desktop/input.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        long long cnt=0;
        int m=pow(2,8)-1;
        for(int i=1;i<=n;i++)
        {
            long long a;
            scanf("%lld",&a);
            for(int i=0;i<4;i++)
            {
                long long temp=a;
                int k=i*8;
                temp=temp>>k;
                temp=temp&m;
                if(temp==97) cnt++;
            }
        }
        printf("%lld\n",cnt);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值