统计元音(hdu 2027)

来源:互联网 发布:dota2天梯淘宝买账号 编辑:程序博客网 时间:2024/05/01 08:01
统计元音
Time Limit : 2000/1000ms (Java/Other) Memory Limit :65536/32768K (Java/Other)
Total Submission(s) : 10 Accepted Submission(s) : 5

Font: Times New Roman | Verdana |Georgia

Font Size:

Problem Description

统计每个元音字母在字符串中出现的次数。

Input

输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。

Output

对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。

请特别注意:最后一块输出后面没有空行:)

Sample Input

2aeioumy name is ignatius

Sample Output

a:1e:1i:1o:1u:1a:2e:1i:3o:0u:1

Author

lcy

Source

C语言程序设计练习(四)

#include <iostream>#include <stdio.h> #include <math.h>     #include <stdio.h>    #include <stdlib.h>    #include <string.h>    #include <algorithm>   using namespace std;int i,j,m;char str[105];char vowel[]="aeiou";int ans[10];int main(){   scanf("%d",&m);   getchar();   while(m--){        gets(str);        memset(ans,0,sizeof(ans));        for(i=0;i<strlen(str);i++)            for(j=0;j<strlen(vowel);j++)                  if(str[i]==vowel[j])                        ans[j]++;       for(i=0;i<5;i++)           printf("%c:%d\n",vowel[i],ans[i]);       if(m>=1)           printf("\n");    }    return0;}


哈希的思想来解此题
#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<time.h>int T , sign[200] ,hash[5]={'a' , 'e', 'i' ,'o' , 'u'};char str[105];int main(){    scanf("%d%*" ,&T );    while( T--)    {        memset( sign ,0 ,sizeof(sign) );        gets( str);        int len = strlen ( str);        for(int i=0 ; i<len ; ++i )            sign[str[i]]++;        for(int i=0 ;i< 5;++i )            printf("%c:%d\n" ,hash[i],sign[hash[i]]);        if( T> 0)            puts("" );    }    return 0;}


0 0
原创粉丝点击