#include
#include
typedef struct DataType
{
int result;
int row,column;
}Data;
typedef struct ListData
{
Data data;
struct ListData *next;
}List;
void Diaohuan(int **a,int **b,int n,int i,int j)
{
int k,l;
if(i>j)
{
k=i;
i=j;
j=k;
}
for(k=0;k
for(l=0;l b[k][l]=a[k][l]; for(l=0;l b[i][l]=a[j][l]; for(k=i+1;k for(l=0;l b[k][l]=a[k][l]; for(l=0;l b[j][l]=a[i][l]; for(k=j+1;k for(l=0;l b[k][l]=a[k][l]; } int Cal(int **a,int n) { int i,j; int up=0,down=0; for(i=0;i for(j=i+1;j up+=a[i][j]; for(i=1;i for(j=0;j
down+=a[i][j]; int result=up-down; if(result<0) return -result; return result; } List * CreateList(int result,int i,int j) { List *h=(List*)malloc(sizeof(List)); h->data.result=result; h->data.row=i; h->data.column=j; h->next=NULL; return h; } void Add(List *h,int result,int i,int j) { List *l=(List*)malloc(sizeof(List)); l->data.result=result; l->data.row=i; l->data.column=j; l->next=NULL; List *l1=h; while(l1->next) l1=l1->next; l1->next=l; } Data Find(List *h) { List *p=h; Data d=p->data; while(p->next) { p=p->next; if(p->data.result>d.result) d=p->data; } return d; } void Free(List *h) { List *p=h; while(h->next) { h=h->next; free(p); p=h; } } int main() { int i,j,n; int **a,**b; scanf("%d",&n); a=(int**)malloc(n*sizeof(int*)); b=(int**)malloc(n*sizeof(int*)); for(i=0;i { a[i]=(int*)malloc(n*sizeof(int)); b[i]=(int*)malloc(n*sizeof(int)); } for(i=0;i for(j=0;j scanf("%d",&a[i][j]); List *h=CreateList(Cal(a,n),0,0); for(i=0;i { for(j=0;j { if(i==0&&j==0) continue; Diaohuan(a,b,n,i,j); Add(h,Cal(b,n),i,j); } } Data data=Find(h); printf("\n\n%d\n\n",data.result); Diaohuan(a,b,n,data.row,data.column); printf("提示:\n\n第%d行与第%d行交换。\n\n",data.row+1,data.column+1); for(i=0;i { for(j=i+1;j { if(i==0&&j==1) printf("|%d",b[i][j]); else printf("+%d",b[i][j]); } } for(i=1;i for(j=0;j
printf("-%d",b[i][j]); printf("|=%d\n\n",data.result); free(a);free(b); Free(h); return 0; }
什么时候要?是求出所有调换组合中的最大差值还是随机调换的最大差值?