时间限制: 1Sec 内存限制: 128MB
题目描述:(原题链接)
给定一段文章,请输出每个字母出现的次数
输入:
只有一组输入数据,该数据大小<10KB。在文章中除最后一个字符外,只有小写字母、空格和换行符,没有另外的标点、数字和大写字母等。该文章以’#’结尾。
样例输入:
here is the input
this is the article#
输出:
输出格式为“C A”,C为’a’…’z’中的字母,A为出现次数,C和A之间空一格
样例输出 :
a 1
b 0
c 1
d 0
e 5
f 0
g 0
h 4
i 5
j 0
k 0
l 1
m 0
n 1
o 0
p 1
q 0
r 2
s 3
t 5
u 1
v 0
w 0
x 0
y 0
z 0
解题思路:
数组存储26个字母出现的次数,endif判断 # 是否出现即是否结束输入
注意事项:
注意字符与数字下标之间的转化
参考代码:
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner in =new Scanner(System.in);
int arr[] = new int[26];//存储每个字母的次数
boolean endif =true;
String s;
while(in.hasNext()) {
if(endif)
s=in.nextLine();
else
break;
for(int i=0;i<s.length();i++) {
if(s.charAt(i)=='#')
endif=false;
if(s.charAt(i)>='a'&&s.charAt(i)<='z') {
int num =s.charAt(i)-'a';
arr[num]++;
}
}
if(!endif)
break;
}
for(int i=0;i<arr.length;i++)
System.out.println((char)('a'+i)+" "+arr[i]);
in.close();
}
}