Find Small A HDU - 5980 java题解

这篇文章描述了一个编程问题,要求在一组32位整数中计算字符a出现的次数。输入包含正整数N和N个正整数,程序通过检查每个整数的每一位,如果等于字符a的ASCII值97,则计数增加。提供的Java代码实现了这一逻辑,通过右移操作处理每一位。

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

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 (a_{i}2^{32}- 1) follow

Output

Output one line,including an integer representing the number of 'a' in the group of given numbers.

Sample

InputOutput
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);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值