鱼C论坛

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

救助,,我的c语言计数和删除好像有些问题

[复制链接]
发表于 2019-2-24 20:25:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 我是个汉子 于 2019-2-24 20:46 编辑

/***********************************************
题目:主函数main()中数字字符串s为测试数据,程序主要先对数
      字字符串s进行降序排序,将结果存入在数组b中,然后对
      数组b中数据进行压缩,不同数字保存在数组b中,数字出
      现次数保存在数组times中,计算各个数字加权和(即数字
      本身乘以该数字出现次数),结果保存在数组c中。

编写程序:
      1. 编写函数void  SortAndChange(char s[],int n, int b[]),
         对字符串s中n个字符进行降序排列,并将n个字符转换为
         数字,保存在数组b中。
   
      2. 编写函数int Calculcate(int b[],int n,int times[],int c[]),
         对数组b中数字进行压缩,不同数字保存在数组b中,不
         同数字出现次数存入数组times中,并计算各个数字加权
         和,结果存入数组c中,函数返回不同数字的个数。
  
      测试数据: 13334444555582298760
      运行结果:b[0]=9 and times[0]=1, the weight sum is 9
                b[1]=8 and times[1]=2, the weight sum is 16
                ...
                b[9]=0 and times[9]=1, the weight sum is 0
*************************************/
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6. #define N 50

  7. void SortAndChange(char s[],int n,int b[])
  8. {
  9. /**********Program**********/
  10. int i,j;
  11. char t;
  12. for(i=0; i<n-1; i++)
  13. {
  14.     for(j=i+1; j<n; j++)
  15.     {
  16.         if(s[i]<s[j])
  17.         {
  18.             t=s[i];
  19.             s[i]=s[j];
  20.             s[j]=t;
  21.         }
  22.     }
  23. }
  24. for(i=0; i<n; i++)
  25.     b[i]=s[i]-'0';
  26. /**********  End  **********/
  27. }
  28. int Calculcate(int b[],int n,int times[],int c[])
  29. {
  30. /**********Program**********/ //98876555544443332210
  31. int i,j,k=0,num,count;
  32. for(i=0; i<n; i++)
  33. {
  34.     count=1;
  35.     for(j=i; j<n; j++)
  36.     {
  37.         if(b[j]==b[j+1])
  38.             count++;
  39.         else
  40.         {
  41.             times[k]=count;
  42.             b[k]=b[j];
  43.             i=j;
  44.             c[k]=b[k]*times[k];
  45.             k++;
  46.             break;
  47.         }   
  48.     }        
  49. }
  50. return k;
  51. /**********  End  **********/
  52. }

  53. int main()
  54. {
  55.     char s[N]="13334444555582298760";
  56.     int b[N]={0};
  57.     int c[10]={0};
  58.     int times[10]={0};
  59.     int num;
  60.     int i,n;
  61.             
  62.     FILE *fp;
  63.     if((fp=fopen("DATA.TXT","w"))==NULL)
  64.     {
  65.         printf("File open error\n");
  66.         exit(0);
  67.     }
  68.         
  69.     n=strlen(s);
  70.     SortAndChange(s,n,b);
  71.     num=Calculcate(b,n,times,c);

  72.     for(i=0;i<num;i++)
  73.     {
  74.         printf("b[%d] = %d and times[%d] =%d, the weight sum is %d\n",i,b[i],i,times[i],c[i]);
  75.         fprintf(fp,"b[%d]= %d and times[%d] =%d, the weight sum is %d\n",i,b[i],i,times[i],c[i]);
  76.     }
  77.    
  78.     fclose(fp);
  79.    
  80.     return 0;
  81. }
复制代码

我的输出:
b[0] = 9 and times[0] =1, the weight sum is 9
b[1] = 8 and times[1] =2, the weight sum is 16
b[2] = 7 and times[2] =1, the weight sum is 7
b[3] = 6 and times[3] =1, the weight sum is 6
b[4] = 5 and times[4] =4, the weight sum is 20
b[5] = 4 and times[5] =4, the weight sum is 16
b[6] = 3 and times[6] =3, the weight sum is 9
b[7] = 2 and times[7] =2, the weight sum is 4
b[8] = 1 and times[8] =1, the weight sum is 1


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-2-25 20:29:57 | 显示全部楼层
  1. if(b[j]==b[j+1])
  2.             count++;
  3.         else
  4.         {
  5.             times[k]=count;
  6.             b[k]=b[j];
  7.             i=j;
  8.             c[k]=b[k]*times[k];
  9.             k++;
  10.             break;
  11.         }   
复制代码

这一段至少有两个问题
1.既然j<n当j=n-1时 b[j+1]溢出,这是不安全的代码
2.考虑下边界情况。你这样写只有b[j]!=b[j+1]时结算,如果最后b[n-1]的值刚好与b[n]这个内存位置储存的值相同,那么最后一次结算就直接被你吃掉了
这就是输出中为什么没有0的原因
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-25 20:57:23 | 显示全部楼层
Croper 发表于 2019-2-25 20:29
这一段至少有两个问题
1.既然j
  1. for(i=0; i<n; i++)
  2. {
  3.         count=1;
  4.         for(j=i; j<n-1; j++)
  5.         {
  6.                 if(b[j]==b[j+1])
  7.                         count++;
  8.                 else
  9.                 {
  10.                         times[k++]=count;
  11.                         b[k]=b[j];
  12.                         i=j;
  13.                         c[k]=b[k]*times[k];
  14.                         break;
  15.                 }       
  16.         }
复制代码

这样修改也不行的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-25 21:09:35 | 显示全部楼层
什么叫“这样修改也不行的”?
那是你自己的代码啊- -,我又没给你改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 07:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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