hdu 1860 统计字符 Java 水题系列

本文介绍了一种利用Java内置函数Arrays.fill()初始化数组的方法,并通过一个具体示例展示了如何计算字符串中各字符出现的次数。该示例程序读取两个字符串,用Arrays.fill()将计数数组初始化为0,然后遍历第二个字符串更新计数,最后输出第一个字符串中每个字符及其在第二个字符串中的出现次数。

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

学会了一个新的函数Arrays.fill();

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1860

import java.io.*;  
import java.math.*;  
import java.util.*;  
      
public class Main{  
    public static void main(String argv[]){  
        Scanner cin = new Scanner(System.in);  
        String s1,s2;
        char [] a1 = new char[1005];
        char [] a2 = new char[1005];
        int  [] ans = new int[300];
        while(cin.hasNext()){
            Arrays.fill(ans,0);
            s1 = cin.nextLine();
            a1 = s1.toCharArray();
            if(a1[0] == '#')break;
            s2 = cin.nextLine();
            a2 = s2.toCharArray();
            for(int i = 0 ; i < s2.length() ; i ++) {
                ans[a2[i]] ++;
            }
            for(int i = 0 ; i < s1.length() ; i ++) {
                System.out.printf("%c %d\r\n",a1[i],ans[a1[i]]);
            }
        }
    }  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值