鱼C论坛

 找回密码
 立即注册
查看: 3048|回复: 2

[已解决]新手求助

[复制链接]
发表于 2021-10-3 11:09:46 | 显示全部楼层 |阅读模式
10鱼币
为什么第二个数据无法输入??

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

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

Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开,请特别注意:最后一块输出后面没有空行

Sample Input
2
aeiou
my name is ignatius

Sample Output
a:1
e:1
i:1
o:1
u:1

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

我的代码如下
  1. #include<stdio.h>
  2. #include<string.h>

  3. int main()
  4. {   char b[101];
  5.     int n=0,f,j;
  6.     int a,e,i,o,u;
  7.    
  8.         while(scanf("%d",&n)!=EOF);
  9.         {
  10.                 getchar();
  11.                 for(int f=0;f<n;f++)
  12.             {   gets(b);
  13.                     a=0, e=0, i=0, o=0, u=0;
  14.                     for(int j=0;j<strlen(b);j++)
  15.                     {
  16.                             if(b[j]=='a'||b[j]=='A')
  17.                             a++;
  18.                             else if(b[j]=='e'||b[j]=='E')
  19.                             e++;
  20.                             else if(b[j]=='i'||b[j]=='I')
  21.                             i++;
  22.                             else if(b[j]=='o'||b[j]=='O')
  23.                             o++;
  24.                             else if(b[j]=='u'||b[j]=='U')
  25.                             u++;
  26.                         }
  27.             }
  28.             if(f==n-1)
  29.             {printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n",a,e,i,o,u);}
  30.             else
  31.             {printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n\n",a,e,i,o,u);}
  32. }

  33. return 0;
  34. }
复制代码
最佳答案
2021-10-3 11:09:47
  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <ctype.h>

  4. int main(void) {
  5.     size_t n; scanf("%lu\n", &n);
  6.     char strings[n][101];
  7.     for(size_t i = 0; i < n; ++i) fgets(strings[i], 101, stdin);
  8.     size_t counts[n][256]; memset(counts, 0, sizeof(counts));
  9.     for(size_t i = 0; i < n; ++i) {
  10.         for(size_t j = 0; strings[i][j]; ++j) {
  11.             ++counts[i][tolower(strings[i][j])];
  12.         }
  13.     }
  14.     const char *new_line = "";
  15.     char vowels[] = {'a', 'e', 'i', 'o', 'u', 0};
  16.     for(size_t i = 0; i < n; ++i) {
  17.         printf("%s", new_line); new_line = "\n";
  18.         for(size_t j = 0; vowels[j]; ++j)
  19.             printf("%c:%lu\n", vowels[j], counts[i][(size_t)vowels[j]]);
  20.     }
  21.     return 0;
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-3 11:09:47 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <memory.h>
  3. #include <ctype.h>

  4. int main(void) {
  5.     size_t n; scanf("%lu\n", &n);
  6.     char strings[n][101];
  7.     for(size_t i = 0; i < n; ++i) fgets(strings[i], 101, stdin);
  8.     size_t counts[n][256]; memset(counts, 0, sizeof(counts));
  9.     for(size_t i = 0; i < n; ++i) {
  10.         for(size_t j = 0; strings[i][j]; ++j) {
  11.             ++counts[i][tolower(strings[i][j])];
  12.         }
  13.     }
  14.     const char *new_line = "";
  15.     char vowels[] = {'a', 'e', 'i', 'o', 'u', 0};
  16.     for(size_t i = 0; i < n; ++i) {
  17.         printf("%s", new_line); new_line = "\n";
  18.         for(size_t j = 0; vowels[j]; ++j)
  19.             printf("%c:%lu\n", vowels[j], counts[i][(size_t)vowels[j]]);
  20.     }
  21.     return 0;
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-3 11:52:28 | 显示全部楼层
本帖最后由 人造人 于 2021-10-3 11:59 编辑

我感觉题目的意思是要你把所有的输入先保存起来,然后计算,然后输出
感觉不是
接收一行输入,计算,输出
然后再接收一行输入,计算,输出
然后再接收一行输入,计算,输出
。。。

  1. #include <stdio.h>
  2. #include <memory.h>

  3. int main(void) {
  4.     size_t n; scanf("%lu\n", &n);
  5.     char strings[n][101];
  6.     for(size_t i = 0; i < n; ++i) fgets(strings[i], 101, stdin);
  7.     size_t counts[n][256]; memset(counts, 0, sizeof(counts));
  8.     for(size_t i = 0; i < n; ++i) {
  9.         for(size_t j = 0; strings[i][j]; ++j) {
  10.             ++counts[i][(size_t)strings[i][j]];
  11.         }
  12.     }
  13.     const char *new_line = "";
  14.     for(size_t i = 0; i < n; ++i) {
  15.         printf("%s", new_line); new_line = "\n";
  16.         printf("%c:%lu\n", 'a', counts[i]['a']);
  17.         printf("%c:%lu\n", 'e', counts[i]['e']);
  18.         printf("%c:%lu\n", 'i', counts[i]['i']);
  19.         printf("%c:%lu\n", 'o', counts[i]['o']);
  20.         printf("%c:%lu\n", 'u', counts[i]['u']);
  21.     }
  22.     return 0;
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 22:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表