以下将做你想要的:
#include <stdlib.h>
#include <stdio.h>
int main(){
int i,v;
int ID_SIZE=7;
int value[]={0,1,2,3,1,4, 7};
int id[]= {1,1,3,3,2,2,10};
//Discover largest and smallest ids in order to allocate memory
int min=0,max=0,length;
for(i=1;i<ID_SIZE;++i){
if(id[i]<id[min]) min=i;
if(id[i]>id[max]) max=i;
}
//Replace ids with values
min=id[min];
max=id[max];
length=max-min+1;
int **unions;
int *append;
append=(int *)calloc(length,sizeof(int));
for(i=0;i<length;++i)
append[i]=-1; //Initial insertion point for each id is at 0
//Create 2D array unions[IDS HERE][VALUES HERE]
unions=(int **)calloc(length,sizeof(int*));
for(i=0;i<length;++i)
unions[i]=(int *)calloc(ID_SIZE,sizeof(int));
//Join arrays
for(i=0;i<ID_SIZE;++i){
printf("Inserting %d into %d at %d\n",value[i],id[i],append[id[i]-min]+1);
unions[id[i]-min][++append[id[i]-min]]=value[i];
}
for(i=0;i<length;++i){
if(append[i]>=0){
printf("Id %d has: ",i+min);
for(v=0;v<=append[id[i]-min];++v)
printf("%d ",unions[i][v]);
printf("\n");
}
}
return 0;
}
它创建了两个动态数组。
一个名为的数组append
跟踪为每个 id 找到了多少值。
另一个数组,称为unions
存储计算结果。
对于我在程序中定义的输入,返回以下内容:
Inserting 0 into 1 at 0
Inserting 1 into 1 at 1
Inserting 2 into 3 at 0
Inserting 3 into 3 at 1
Inserting 1 into 2 at 0
Inserting 4 into 2 at 1
Inserting 7 into 10 at 0
Id 1 has: 0 1
Id 2 has: 1 4
Id 3 has: 2 3
Id 10 has: 7