- 论坛徽章:
- 0
|
#include <stdio.h>
int c[20];
int mcam(int *a,int *b)
{
if( *a > *b)
return 1;
else if( *a == *b)
return 0;
else
return -1;
}
int printarr(int *a,int size)
{
int i ;
for(i = 0;i < size;i++){
printf("%d,",*a);
a++;
}
printf("\n");
}
int a[] = {10,34,230,400,321,4233,2000,50,320,333};
int b[] = {23,7000,200,634,987,230,640,46,135,257};
int main(int argc,char **argv)
{
qsort(a,10,sizeof(int),mcam);
qsort(b,10,sizeof(int),mcam);
printarr(a,10);
printarr(b,10);
int loop = 1;
int suma,sumb;
int gap;
int max,temp;
int i,j;
int sub;
while(loop){
int chgi=-1,chgj=-1;
max = 0;
suma=sum(a,10);
sumb=sum(b,10);
gap = (suma-sumb)/2;
for(i=9;i>=0;i--){
for(j=0;j<10;j++){
sub = a[i] - b[j];
if(gap > 0){
if(max <sub && sub <gap){
max = sub;
chgi = i;
chgj = j;
}
}else if(gap < 0){
if( max >sub && sub >gap){
max = sub;
chgi = i;
chgj = j;
}
}else{
printf("loop = 0\n");
loop =0;
break;
}
}
}
printf("gap=%d i=%d,j=%d\tmax=%d\n",gap,chgi,chgj,max);
if(chgi == -1){
printf("none change\n");
break;
}
temp = a[chgi];
a[chgi] = b[chgj];
b[chgj]=temp;
qsort(a,10,sizeof(int),mcam);
qsort(b,10,sizeof(int),mcam);
printf("suma=%d,sumb=%d\n",sum(a,10),sum(b,10));
}
printf("suma=%d,sumb=%d\n",sum(a,10),sum(b,10));
printarr(a,10);
printarr(b,10);
return 0;
}
int sum(int *a,int size)
{
int i;
int sum=0;
for(i=0;i<size;i++){
sum+=*a;
a++;
}
return sum;
} |
|