习题描述:
将一个5*5的矩阵中最大的元素放在中心,四个角分别放四个最小的元素(顺序从左到右,从上到下顺序依次从小到大存放),写一个函数实现之。用main函数调用。
例如原来矩阵:
35 34 33 32 31
30 29 28 27 26
25 24 23 22 21
20 19 18 17 16
15 14 13 12 11
改变后的矩阵
11 34 33 32 12
30 29 28 27 26
25 24 35 22 21
20 19 18 17 16
13 23 15 31 14
程序好如下:
#include "stdafx.h"
int main(int argc, char* argv[])
{
int *p = NULL;
p = (int *)malloc(sizeof(int)*25);
printf("Please input 25 number:/n");
int main(int argc, char* argv[])
{
int *p = NULL;
p = (int *)malloc(sizeof(int)*25);
printf("Please input 25 number:/n");
//input
for(int i = 0 ;i<25; i++)
{
scanf("%d",&p[i]);
}
//output
printf("Array A:/n");
for(int j = 0;j<25;j++)
{ if(j%5==0)
printf("/n");
printf("%-4d",p[j]);
}
printf("/n");
//copy
int *s = NULL;
s = (int *)malloc(sizeof(int)*25);
for(i=0;i<25;i++)
{
s[i]=p[i];
}
int *s = NULL;
s = (int *)malloc(sizeof(int)*25);
for(i=0;i<25;i++)
{
s[i]=p[i];
}
//sort
int x;
for(i=1;i<25;i++)
{
for(j=0;j<25-i;j++)
{
if(s[j]>s[j+1])
{
x = s[j];
s[j] = s[j+1];
s[j+1] = x;
}
}
}
//search minnumber
int temp;
for(i=0;i<4;i++)
{
for(j=0;j<25;j++)
{
if((s[i]-p[j])==0)
break;
}
if((s[0]-p[j])==0)
{
temp=p[0];
p[0]=p[j];
p[j]=temp;
}
else
if((s[1]-p[j])==0)
{
temp=p[4];
p[4]=p[j];
p[j]=temp;
}
else
if((s[2]-p[j])==0)
{
temp=p[20];
p[20]=p[j];
p[j]=temp;
}
else
if((s[3]-p[j])==0)
{
temp=p[24];
p[24]=p[j];
p[j]=temp;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<25;j++)
{
if((s[i]-p[j])==0)
break;
}
if((s[0]-p[j])==0)
{
temp=p[0];
p[0]=p[j];
p[j]=temp;
}
else
if((s[1]-p[j])==0)
{
temp=p[4];
p[4]=p[j];
p[j]=temp;
}
else
if((s[2]-p[j])==0)
{
temp=p[20];
p[20]=p[j];
p[j]=temp;
}
else
if((s[3]-p[j])==0)
{
temp=p[24];
p[24]=p[j];
p[j]=temp;
}
}
//search maxnumber
int temp2;
for(j=0;j<25;j++)
if((s[24]-p[j])==0)
{
temp2=p[12];
p[12]=p[j];
p[j]=temp2;
}
int temp2;
for(j=0;j<25;j++)
if((s[24]-p[j])==0)
{
temp2=p[12];
p[12]=p[j];
p[j]=temp2;
}
//finish
printf("Array B:/n");
for(j = 0;j<25;j++)
{ if(j%5==0)
printf("/n");
printf("%-4d",p[j]);
}
printf("/n");
return 0;
}
printf("Array B:/n");
for(j = 0;j<25;j++)
{ if(j%5==0)
printf("/n");
printf("%-4d",p[j]);
}
printf("/n");
return 0;
}