没注释的源代码
#include<iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void transform(int *arry, int col_row);
int main()
{
int arry[5][5];
cout<<"Please enter a 5x5 matrix:"<<endl;
for (int i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
cin>>arry[i][j];
}
}
transform(*arry, 5);
cout<<"now matrix:"<<endl;
for (int i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
cout<<arry[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
void transform(int *arry,int col_row)
{
int max =arry[0],max_idx;
for (int i=0;i<col_row*col_row;i++)
{
if (max < arry[i]) max=arry[i];
max_idx=i;
}
int center_idx=(col_row*col_row)/2;
int tmp=arry[center_idx];arry[center_idx]=arry[max_idx];arry[max_idx]=tmp;
int min_idx[4];
int i,j,k;
for (i=0;i<4;i++)
{
int min_tmp=arry[col_row*col_row-1];
for (j=0;j<col_row*col_row;j++)
{
for (k=0;k<i;k++)
{
if (j==min_idx[k]) break;
}
if(k!=i) continue;
if(min_tmp>arry[j])
{
min_tmp=arry[j];
min_idx[i]=j;
}
}
}
int change_idx[4];
change_idx[0]=0;
change_idx[1]=col_row-1;
change_idx[2]=col_row*(col_row-1);
change_idx[3]=(col_row*col_row)-1;
for (int i=0;i<4;i++)
{
int tmp;
tmp=arry[change_idx[i]];
arry[change_idx[i]]=arry[min_idx[i]];
arry[min_idx[i]] = tmp;
}
}