鱼C论坛

 找回密码
 立即注册
查看: 1578|回复: 3

[已解决]S1E19课后作业3

[复制链接]
发表于 2021-10-23 14:02:44 | 显示全部楼层 |阅读模式
10鱼币
字符统计个数:
对于
要求 B:统计不同的字符个数,并打印出来;
要求 C:找出出现次数最多的字符。

我的思路:分别用两个数组把不同的字符和其对应的出现次数保存起来(对应的下标一样),
可是中间的循环好像出了问题,希望大佬帮助。

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

  3. int main(void)
  4. {
  5.         char str1[256];
  6.         char str2[256] = {'0'};
  7.         int count[256] = {0};
  8.         int count1 = 0,i = 0,j = 0,k = 0,count2 = 0,max = 0;
  9.        
  10.         printf("请输入英文文本:");
  11.         while ((str1[i++] = getchar()) != '\n')
  12.         {
  13.                 count1++;
  14. }
  15.         //printf("%s\n",str1);
  16.         --i;
  17.         str2[0] = str1[0];
  18.         for (k = 1;k < i;k++)
  19.         {
  20.                 for (j = 0; j < k; j++)
  21.                 {
  22.                         if (str1[j] == str1[k])
  23.                         {
  24.                                 count[j] += 1;
  25.                                 break;
  26.                         }
  27.                         if (j == k - 1)
  28.                         {
  29.                                 str2[k] = str1[k];
  30.                                 count[k] = 1;
  31.                                 count2++;
  32.                         }
  33.                 }
  34.         }
  35.        
  36.         printf("你总共输入了%d个字符,其中的不同的字符有%d个\n",count1,count2);
  37.         printf("%s\n",str2);
  38.         printf("它们是:");
  39.        
  40.         for (k = 0;k < i;k++)
  41.         {
  42.                 if (str2[k] != '0')
  43.                         putchar(str2[k]);
  44.         }
  45.         putchar('\n');
  46.        
  47.         for (k = 1;k < i;k++)
  48.         {
  49.                 if (count[k] >= count[k - 1])
  50.                         max = count[k];
  51.                         j = k;
  52.         }
  53.        
  54.         printf("出现次数最多的字符是:'%c',它总共出现了%d次。",str2[j],max);
  55.        
  56.     return 0;
  57. }
复制代码


应该是中间这部分错了,但是我没想明白
  1. str2[0] = str1[0];
  2.         for (k = 1;k < i;k++)
  3.         {
  4.                 for (j = 0; j < k; j++)
  5.                 {
  6.                         if (str1[j] == str1[k])
  7.                         {
  8.                                 count[j] += 1;
  9.                                 break;
  10.                         }
  11.                         if (j == k - 1)
  12.                         {
  13.                                 str2[k] = str1[k];
  14.                                 count[k] = 1;
  15.                                 count2++;
  16.                         }
  17.                 }
  18.         }
复制代码
最佳答案
2021-10-23 14:02:45
  1. #include <stdio.h>
  2. #define ASCII 128


  3. int main(void) {


  4.     int total[ASCII] = {0};//一个长度位123位,可以放下ASCII码0-122位的数组,每一位代表字符出现频率
  5.     int sum = 0;//总共出现多少个字符
  6.     int difference = 0;//不同的字符的数量


  7.     printf("请输入英文文本:");


  8.     char ch;


  9.     while((ch = getchar()) != '\n') {//只要没有输入结束,每个字符都拿到
  10.         total[ch]++;//字符所在的ASCII码对应数组位置,上面的数量+1
  11.         sum++;
  12.     }


  13.     for(int i = 0; i < ASCII; i++) {
  14.         //printf("%d",total[i]);
  15.         if (total[i] != 0) { //只要出现过的字符,数字必定不等于零,所以difference+1
  16.             difference++;
  17.         }
  18.     }


  19.     printf("你总共输入了%d个字符,其中不同的字符个数有%d个。\n它们是:",sum,difference);
  20.    
  21.     int max = 0;//出现最多的字符的次数
  22.     int max_i = 0;//出现最多的字符所在数组的位置


  23.     for(int i = 0; i < ASCII; i++) {
  24.         if (total[i] !=  0) {
  25.             printf("%c",i);//每一个不同的字符都打印出来


  26.             if (total[i] > max) { //出现频率更多的,就把频率记录下来
  27.                 max = total[i];
  28.                 max_i = i; //最高频率的位置记录下来
  29.             }


  30.         }
  31.     }


  32.     putchar('\n');


  33.     printf("出现次数最多的字符是\'%c\',它总共出现了%d次。\n",max_i,max);   


  34.     return 0;
  35. }
复制代码


这是我的答案

最佳答案

查看完整内容

这是我的答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-23 14:02:45 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #define ASCII 128


  3. int main(void) {


  4.     int total[ASCII] = {0};//一个长度位123位,可以放下ASCII码0-122位的数组,每一位代表字符出现频率
  5.     int sum = 0;//总共出现多少个字符
  6.     int difference = 0;//不同的字符的数量


  7.     printf("请输入英文文本:");


  8.     char ch;


  9.     while((ch = getchar()) != '\n') {//只要没有输入结束,每个字符都拿到
  10.         total[ch]++;//字符所在的ASCII码对应数组位置,上面的数量+1
  11.         sum++;
  12.     }


  13.     for(int i = 0; i < ASCII; i++) {
  14.         //printf("%d",total[i]);
  15.         if (total[i] != 0) { //只要出现过的字符,数字必定不等于零,所以difference+1
  16.             difference++;
  17.         }
  18.     }


  19.     printf("你总共输入了%d个字符,其中不同的字符个数有%d个。\n它们是:",sum,difference);
  20.    
  21.     int max = 0;//出现最多的字符的次数
  22.     int max_i = 0;//出现最多的字符所在数组的位置


  23.     for(int i = 0; i < ASCII; i++) {
  24.         if (total[i] !=  0) {
  25.             printf("%c",i);//每一个不同的字符都打印出来


  26.             if (total[i] > max) { //出现频率更多的,就把频率记录下来
  27.                 max = total[i];
  28.                 max_i = i; //最高频率的位置记录下来
  29.             }


  30.         }
  31.     }


  32.     putchar('\n');


  33.     printf("出现次数最多的字符是\'%c\',它总共出现了%d次。\n",max_i,max);   


  34.     return 0;
  35. }
复制代码


这是我的答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-25 10:38:33 | 显示全部楼层

我想出来了,是这样,谢谢你
  1. #include <stdio.h>
  2. #include <string.h>

  3. int main(void)
  4. {
  5.         char str1[256];
  6.         char str2[256];
  7.         memset(str2,'0',256);
  8.         int count[256] = {0};
  9.         int count1 = 0,i = 0,j = 0,k = 0,count2 = 1,max = 0;
  10.        
  11.         printf("请输入英文文本:");
  12.         while ((str1[i++] = getchar()) != '\n')
  13.         {
  14.                 count1++;
  15.     }
  16.         --i;
  17.         str2[0] = str1[0];
  18.         count[0] = 1;
  19.         for (k = 1;k < i;k++)
  20.         {
  21.                 for (j = 0; j < k; j++)
  22.                 {
  23.                         if (str1[j] == str1[k])
  24.                         {
  25.                                 count[j] += 1;
  26.                                 break;
  27.                         }
  28.                         if (j == k - 1)
  29.                         {
  30.                                 str2[k] = str1[k];
  31.                                 count[k] = 1;
  32.                                 count2++;
  33.                         }
  34.                 }
  35.         }
  36.        
  37.         printf("你总共输入了%d个字符,其中的不同的字符有%d个\n",count1,count2);
  38.         printf("它们是:");
  39.        
  40.         for (k = 0;k < i;k++)
  41.         {
  42.                 if (str2[k] != '0')
  43.                         putchar(str2[k]);
  44.         }
  45.         putchar('\n');
  46.        
  47.         max = count[0];
  48.         j = 0;
  49.         for (k = 1;k < i;k++)
  50.         {
  51.                 if (count[k] >= max)
  52.                 {
  53.                         max = count[k];
  54.                         j = k;
  55.                 }
  56.         }
  57.        
  58.         printf("出现次数最多的字符是:'%c',它总共出现了%d次。",str2[j],max);
  59.        
  60.     return 0;
  61. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-10-25 16:41:51 | 显示全部楼层
飞花落尽 发表于 2021-10-25 10:38
我想出来了,是这样,谢谢你

不客气,互相帮助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 15:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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