首页 > 代码库 > HDU5980

HDU5980

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).

InputThe 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) followOutputOutput 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
 题意:给N个数 每个数都可以拆开成一个32位的2进制 每八位一个字节  每个字节的2进制数换算成十进制的看有多少个97

 

思路:CillyB: % 256是看看后8位是多少, /= 256是去掉后8位

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int ans,n,x;
 8     int i;
 9     cin>>n;
10     ans=0;
11     for(i=0;i<n;i++)
12     {
13         scanf("%d",&x);
14         while(x)
15         {
16             if(x%256==97)
17             {
18                 ans++;
19             }
20             x/=256;
21         }
22     }
23     cout<<ans<<endl;
24 }

 

HDU5980