鱼C论坛

 找回密码
 立即注册
查看: 2198|回复: 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
*************************************/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#define N 50

void SortAndChange(char s[],int n,int b[])
{
/**********Program**********/
int i,j;
char t;
for(i=0; i<n-1; i++)
{
    for(j=i+1; j<n; j++)
    {
        if(s[i]<s[j])
        {
            t=s[i];
            s[i]=s[j];
            s[j]=t;
        }
    }
}
for(i=0; i<n; i++)
    b[i]=s[i]-'0';
/**********  End  **********/
}
int Calculcate(int b[],int n,int times[],int c[])
{
/**********Program**********/ //98876555544443332210
int i,j,k=0,num,count;
for(i=0; i<n; i++)
{
    count=1;
    for(j=i; j<n; j++)
    {
        if(b[j]==b[j+1])
            count++;
        else 
        {
            times[k]=count;
            b[k]=b[j];
            i=j;
            c[k]=b[k]*times[k];
            k++;
            break;
        }    
    }        
}
return k;
/**********  End  **********/
}

int main()
{
    char s[N]="13334444555582298760";
    int b[N]={0};
    int c[10]={0};
    int times[10]={0};
    int num;
    int i,n;
            
    FILE *fp;
    if((fp=fopen("DATA.TXT","w"))==NULL)
    {
        printf("File open error\n");
        exit(0);
    }
        
    n=strlen(s);
    SortAndChange(s,n,b);
    num=Calculcate(b,n,times,c);

    for(i=0;i<num;i++)
    {
        printf("b[%d] = %d and times[%d] =%d, the weight sum is %d\n",i,b[i],i,times[i],c[i]);
        fprintf(fp,"b[%d]= %d and times[%d] =%d, the weight sum is %d\n",i,b[i],i,times[i],c[i]);
    }
   
    fclose(fp);
    
    return 0;
}
我的输出:
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


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

使用道具 举报

发表于 2019-2-25 20:29:57 | 显示全部楼层
if(b[j]==b[j+1])
            count++;
        else 
        {
            times[k]=count;
            b[k]=b[j];
            i=j;
            c[k]=b[k]*times[k];
            k++;
            break;
        }    

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

使用道具 举报

 楼主| 发表于 2019-2-25 20:57:23 | 显示全部楼层
Croper 发表于 2019-2-25 20:29
这一段至少有两个问题
1.既然j
for(i=0; i<n; i++)
{
        count=1;
        for(j=i; j<n-1; j++)
        {
                if(b[j]==b[j+1])
                        count++;
                else 
                {
                        times[k++]=count;
                        b[k]=b[j];
                        i=j;
                        c[k]=b[k]*times[k];
                        break;
                }        
        }
这样修改也不行的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-25 21:09:35 | 显示全部楼层
什么叫“这样修改也不行的”?
那是你自己的代码啊- -,我又没给你改
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 08:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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