请问,这个关卡怎么写啊,C语言指针-矩阵变幻相关的。写个思路或者交换函数比如这种类型的
关卡要求
swap那么写没问题,其他几个操作和可以封装成函数。
运行结果:
代码:
#include <stdio.h>
int A[105][105];
void swap(int *a,int* b)
{
int t = *a;
*a = *b;
*b = t;
}
//行翻转
void row_invers(int x,int l,int r)
{
int i=l-1,j=r-1,t;
x--;
for(;i<j;i++,j--)
{
t = A[x][i];
A[x][i] = A[x][j];
A[x][j] = t;
}
}
//列翻转
void col_inverse(int y,int l,int r)
{
int i=l-1,j=r-1;
int t;
y--;
for(;i<j;i++,j--)
{
t = A[i][y];
A[i][y] = A[j][y];
A[j][y] = t;
}
}
//大小排序
void sort(int x1,int y1,int x2,int y2,int x3,int y3)
{
int a = A[x1-1][y1-1];
int b = A[x2-1][y2-1];
int c = A[x3-1][y3-1];
int max = a>(b>c?b:c)?a:(b>c?b:c);
int min = a<(b<c?b:c)?a:(b<c?b:c);
int mid = (a+b+c)-max-min;
A[x1-1][y1-1] = min;
A[x2-1][y2-1] = mid;
A[x3-1][y3-1] = max;
}
int main()
{
int i,j;
int n,m,q;
int op,x,y,l,r;
int x1,y1,x2,y2,x3,y3;
scanf("%d %d %d",&n,&m,&q);
//读取n*m个数
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&A[i][j]);
}
//输入q次操作
for(i=0;i<q;i++)
{
scanf("%d",&op);
if(op == 1)
{
scanf("%d %d %d",&x,&l,&r);
row_invers(x,l,r);
}else if(op == 2)
{
scanf("%d %d %d",&y,&l,&r);
col_inverse(y,l,r);
}else if(op == 3)
{
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
sort(x1,y1,x2,y2,x3,y3);
}
}
//输出
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(j<m-1)
printf("%d ",A[i][j]);
else
printf("%d\n",A[i][j]);
}
}
return 0;
}