2024-05-19 00:52:50
代码如下 供参考
#include <stdio.h>
int main()
{
int a[5][5];
int i,j;
int maxi,maxj, mini,minj;
for(i = 0; i < 5; i ++)
for(j = 0; j < 5; j ++)
scanf("%d",&a[i][j]);
maxi = maxj = mini = minj = 0;
for(i = 0; i < 5; i ++)
for(j = 0; j < 5; j ++)
{
if(a[i][j] < a[mini][minj])
{
mini = i;
minj = j;
}
if(a[i][j] > a[maxi][maxj])
{
maxi = i;
maxj = j;
}
}
i = a[maxi][maxj];
a[maxi][maxj] = a[2][2];
a[2][2] = i;
i = a[mini][minj];
a[mini][minj] = a[0][0];
a[0][0] = i;
for(i = 0; i < 5; i ++)
{
for(j = 0; j < 5; j ++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
2024-05-19 03:44:32
2024-05-19 02:50:44
//刚写的code,测试通过,如果有疑问,欢迎交流
//
#include<stdio.h>
#include<stdlib.h>
int main(){
int tar[5][5];
int i,j;
for(i = 0; i<5;i++){
for(j = 0; j<5; j++){
//scanf("%d", &tar[i][j]);
tar[i][j] = rand()%20; //随机生成的数字,测试用
}
}
//保存最大值,及最大的横纵坐标
int max_value = tar[0][0], max_idx_i = 0, max_idx_j = 0;
//保存最小值,及最小值的横纵坐标
int min_value = tar[0][0], min_idx_i = 0, min_idx_j = 0;
for(i =0; i<5; i++){
for(j = 0; j < 5; j++ ){
if(tar[i][j] > max_value){
max_value = tar[i][j];
max_idx_i = i;
max_idx_j = j;
}
if(tar[i][j] < min_value){
min_value = tar[i][j];
min_idx_i = i;
min_idx_j = j;
}
}
}
//交换
tar[max_idx_i][max_idx_j] = tar[2][2];
tar[2][2] = max_value;
tar[min_idx_i][min_idx_j] = tar[0][0];
tar[0][0] = min_value;
for(i = 0; i<5; i++){
for(j = 0; j<5; j++){
printf("%3d", tar[i][j]);
}
printf("\n");
}
return 0;
}