九度OJ 1098:字母统计 (计数)

来源:互联网 发布:linux 内存使用情况 编辑:程序博客网 时间:2024/05/22 23:47

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3720

解决:1809

题目描述:

输入一行字符串,计算其中A-Z大写字母出现的次数

输入:

案例可能有多组,每个案例输入为一行字符串。

输出:

对每个案例按A-Z的顺序输出其中大写字母出现的次数。

样例输入:
DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK
样例输出:
A:0B:0C:0D:3E:2F:5G:0H:0I:1J:4K:2L:3M:0N:1O:0P:0Q:1R:0S:2T:0U:0V:0W:1X:0Y:0Z:0
来源:
2009年上海交通大学计算机研究生机试真题

思路:

另找一个数组,计数。


代码:

#include <stdio.h>#include <string.h> #define N 26 int main(void){    int i;    char s[1000];    int count[N];     while (scanf("%s", s) != EOF)    {        memset(count, 0, N*sizeof(int));        for(i=0; s[i]; i++)        {            if ('A' <= s[i] && s[i] <= 'Z')                count[s[i]-'A'] ++;        }        for (i=0; i<N; i++)            printf("%c:%d\n", i+'A', count[i]);    }     return 0;}/**************************************************************    Problem: 1098    User: liangrx06    Language: C    Result: Accepted    Time:0 ms    Memory:912 kb****************************************************************/


0 0