2016ICPC大连站 J Find Small A【water】

来源:互联网 发布:人类实验室 网络暴力 编辑:程序博客网 时间:2024/05/22 15:46

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≤ aiai≤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

把32位的数字分成 四个八位数字 计算这四个二进制数 有几个97
最后sum求和

#include<iostream>#include<cstring>#include<algorithm>using namespace std;int a[64];int b[64];int c[8]={1,0,0,0,0,1,1,0};int n;int t;int k=0;void solve(){    memset(a,0,sizeof(a));    memset(b,0,sizeof(b));    while(n)    {       a[k++]=n%2;       n/=2;    }    for(int i=0;i<32;i++)    {        b[i]=a[i];    }}int main(){    while(cin>>t)    {        int ans=0;        while(t--)        {             cin>>n;            k=0;            solve();bool flag1=1,flag2=1,flag3=1,flag4=1;            for(int i=0;i<8;i++)                if(b[i]!=c[i])                    flag1=0;            for(int i=8;i<16;i++)                if(b[i]!=c[i-8])                    flag2=0;            for(int i=16;i<24;i++)                if(b[i]!=c[i-16])                    flag3=0;            for(int i=24;i<32;i++)                if(b[i]!=c[i-24])                    flag4=0;            ans+=flag1+flag2+flag3+flag4;        }        cout<<ans<<endl;    }    return 0;}
原创粉丝点击