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) follow
Output
Output one line,including an integer representing the number of 'a' in the group of given numbers.
Sample
Input | Output |
---|---|
3 97 24929 100 | 3 |
Translate:
众所周知,字符“a”的 ASCII 是 97。现在,找出一组给定数字中有多少个字符“a”。请注意,这里的数字由计算机中的 32 位整数给出。这意味着,1 位表示 4 个字符(一个字符由 8 位的二进制数字表示)。
输入包含一组测试数据。第一个数字是一个正整数 N (1≤N≤100),然后是 N 个正整数 (1≤ai≤2^32 - 1 )
输出一行,包括一个整数,表示给定数字组中“a”的数量。
PS:
这题之前用C++写过,今天刚学java,发现java 有byte 强制转换这个东西,不再用java写一下这题简直浪费!所以他来了,还是每次只看8位,如果是97,就记一个,然后把这8位移走;
AC代码:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
while(n!=0)
{
n--;
int a=sc.nextInt();
while(a!=0)
{
if((byte) a==97)sum++;
a>>=8;
}
}
sc.close();
System.out.println(sum);
}
}