|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
|
|