您的位置:首页 > 其它

hdu 1219 AC Me

2015-06-10 22:42 267 查看
题意:水题。。。就是求a-z字母出现的次数。注意每个测试事件之间有一个空白行。

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] mat = new int[26];
		int temp = 0;
		while(sc.hasNext()){
			String test = sc.nextLine();
			//初始化为零
			for (int i = 0; i < mat.length; i++) {
				mat[i] = 0;
			}
			for (int i = 0; i < test.length(); i++) {
				char ch = test.charAt(i);
				if(ch<='z'&&ch>='a'){
					temp = (int)(ch-'a');
					mat[temp]++;
				}
			}
			for (int i = 0; i < mat.length; i++) {
				System.out.println((char)('a'+i)+":"+mat[i]);
			}
			System.out.println();//这个注意
		}
	}

}


c语言代码:

#include<iostream>
#include<string>
using namespace std;

int main(){
    string str;
    while(getline(cin,str)){
        int a[26];
        for(int i=0;i<26;i++){
            a[i] = 0;
        }
        for(int i=0;i<str.length();i++){
            if(islower(str[i])){
                a[str[i]-'a']++;
            }
        }
        for(int i=0;i<26;i++){
            cout<<char(i+'a')<<":"<<a[i]<<endl;
        }
        cout<<endl;
    }
    return 0;
}


AC Me

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 13911 Accepted Submission(s): 6110



Problem Description
Ignatius is doing his homework now. The teacher gives him some articles and asks him to tell how many times each letter appears.

伊格内修斯现在正在做作业。他的老师给了他几篇文章并且要求他求出每篇文章每个字母出现的次数。

It's really easy, isn't it? So come on and AC ME.

真的很容易,对吧!来!让我们解决他。

Input
Each article consists of just one line, and all the letters are in lowercase. You just have to count the number of each letter,
每一篇文章只有一行,输入的字母都是小写。你的任务是计算每个字母出现的次数。
so do not pay attention to other characters. The length of article is at most 100000. Process to the end of file.

所以没有必要去考虑其他的字母。文章的长度小于等于10万。直接输入到文件末尾。

Note: the problem has multi-cases, and you may use "while(gets(buf)){...}" to process to the end of file.

注意:问题有多个,你可以用"while(gets(buf)){...}" 语句去处理问题知道文件末尾。

Output
For each article, you have to tell how many times each letter appears. The output format is like "X:N".

对于每一篇文章,你必须计算出每个字母出现的次数。输入的格式如下例子所示。

Output a blank line after each test case. More details in sample output.

每个测试事件用一个空白行隔离。

Sample Input
hello, this is my first acm contest!
work hard for hdu acm.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: