#二维数组的选择排序

//九宫格内按顺时针进行的选择排序
#include<stdio.h>
void swap(int w, int q);
void zhaozuobiao(int wu[3][3]);//交换零与中间的数
void chu(int wu[3][3], int n);//交换n与中间坐标的值
void shunshizhen(int wu[3][3], int m, int n);//选出指定数的坐标的顺时针的下一位并与中间坐标交换
int main()
{
int wu[3][3];
int n, m, i, t;
for (i = 0; i < 3; i++)
{
scanf_s("%d", wu[0][i]);
}
scanf_s("%d", wu[1][0]);
scanf_s("%d", wu[1][2]);
for (i = 0; i < 3; i++)
{
scanf_s("%d", wu[2][i]);
}
wu[1][1] = 0;
for (m = 0; m < 3; m++)
{
for (n = 0; n < 3; n++)
{
if (wu[m][n] == 1)
break;
}
if (wu[m][n] == 1)
break;
}
for (t = 2; t < 9; t++)
{
chu(wu, t);
shunshizhen(wu, m, n);
zhaozuobiao(wu);
for (m = 0; m < 3; m++)
{
printf("%d", wu[m]);
}
}

return 0;
}
void shunshizhen(int wu[3][3], int m, int n)//选出指定数的坐标的顺时针的下一位并与中间坐标交换
{
int w = m, c = n;
if (m == 0 && n < 2)
n++;
else if (m == 0 && n == 2)
{
m++;
}
else if (m == 1 && n == 0)
{
m–;
}
else if (m == 1 && n == 2)
{
m++;
}
else if (m == 2 && n > 0)
{
n–;
}
else if (m == 2 && n == 0)
{
m–;
}
swap(&wu[1][1], &wu[m][n]);
}
void chu(int wu[3][3], int n)//交换n与中间坐标的值
{
int x, y;
for (x = 0; x < 3; x++)
{
for (y = 0; y < 3; y++)
{
if (wu[x][y] == n)
swap(&wu[1][1], &n);
}
}
}
void zhaozuobiao(int wu[3][3])//交换零与中间的数
{
int m, n;
for (m = 0; m < 3; m++)
{
for (n = 0; n < 3; n++)
{
if (wu[m][n] == 0)
swap(&wu[m][n], &wu[1][1]);
}
}
}
void swap(int *w, int *q)
{
int temp;
temp = *w;
*w = *q;
*q = temp;
}`

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页