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

被折叠的 条评论
为什么被折叠?



