hdu5980—Find Small A(进制)

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/xianpingping/article/details/82934278

Find Small A

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


 

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

2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)

题意:

就是转化成二进制后,每8位看是否和97的二进制一样。

有几个ans加几个。

想来自己的方法也有点愚蠢:

#include<bits/stdc++.h>
using namespace std;
const int maxn=100000;
typedef long long LL;
const int inf=0x3f3f3f3f;
int w[100];
int lala(int n){
    int cnt=0;
    while(n){
        if(n%2==0)
           w[++cnt]=0;
        else
            w[++cnt]=1;
        n=n/2;
    }
   /* for(int i=1;i<=cnt;i++){
        cout<<w[i]<<" ";
    }
    cout<<endl;
*/
    return cnt;
}
int main()
{
    int n,x;
    scanf("%d",&n);
     int num=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        int haha=lala(x);
        for(int j=1;j<=haha;){///一共有haha位
            int k=j;
            if(k+6<=haha)
            {
                if( w[k]==1 &&w[k+1]==0 &&w[k+2]==0 &&w[k+3]==0 &&w[k+4]==0 &&w[k+5]==1 &&w[k+6]==1 && w[k+7]==0 )
                num++;
            }
            j=j+8;
        }
    }
     cout<<num<<endl;
}

简单解法:

让ans=1<<8然后用这个数取余看是否等于97,然后右移8位。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,x;
    int num;
    int ans=1<<8;
    while(scanf("%d",&n)!=EOF){
        num=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        while(x){
            if(x%ans==97)
                num++;
            x>>=8;
        }
    }
    printf("%d\n",num);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/82934278
今日推荐