using namespace std;
#define N 3
int i, j;
void func(int array[][N], int n)
{
int array_temp[N*N];
int k = 0, temp;
//二维数组转换为一维数组
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
{
array_temp[k] = array[i][j];
k++;
}
//对一维数组进行冒泡排序
for (i = 0; i < n * n - 1; ++i)
for (j = 0; j < n * n - 1 - i; ++j)
if (array_temp[j] < array_temp[j + 1])
{
temp = array_temp[j];
array_temp[j] = array_temp[j + 1];
array_temp[j + 1] = temp;
}
//再重新将排序的一维数组转换为二维数组
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
array[i][j] = array_temp[i * N + j];
}
int main()
{
int array[N][N];
cout<<"input the matrix:"<<endl;
for (i = 0; i < N; ++i)
for (j = 0; j < N; ++j)
cin>>array[i][j];
cout<<"the matrix:"<<endl;
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
cout<<array[i][j]<<" ";
cout<<endl;
}
func(array,N);
cout<<"the sorted matrix:"<<endl;
for (i = 0; i < N; ++i)
{
for (j = 0; j < N; ++j)
cout<<array[i][j]<<" ";
cout<<endl;
}
return 0;
}
算法描述:一个N*N的矩阵,调整其数据位置,让其每个位置的值大于等于右值,且大于等于下值(边界除外)#include using namespace std;#define N 3int i, j;void func(int array[][N], int n){ int array_temp[N*N]; int k = 0, temp; //二维数组转换为一维数组 f