#include<stdio.h>
#define spt 10
void NhapMaTran(int a[][spt], int& n)
{
printf("\nNhap so phan tu: "); for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
printf("\nNhap phan tu a[%d][%d]=", i
, j
); }
}
void XuatMaTran(int a[][spt], int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
}
}
void MaTranSangMang(int a[][spt], int b[], int n) //ham chuyen ma tran sang mang
{
int k=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
b[k++]=a[i][j];
}
void HoanVi(int& a, int& b)
{
int temp=a;
a=b;
b=temp;
}
void BubbleSort(int b[], int n)
{
for(int i=0; i<n-1; i++)
for(int j=n-1; j>=i; j--)
if(b[j]<b[i])
HoanVi(b[i], b[j]);
}
void SapXepLenXuong1(int a[][spt], int b[], int n) //ham sap xep ca phan tu huong len
{
int dong, dong1, cot, k=0;
for(dong=0; dong<n; dong++) //quet tu dong dau tien toi dong cuoi cung
for(dong1=dong; dong1>=0; dong1--)//quet tu dong hien tai nguoc tro len dong dau tien
for(cot=0; cot<n; cot++)//quet cac cot
if(dong1+cot==dong)//dieu kien de dien so vao ma tran
a[dong1][cot]=b[k++];
for(dong=1; dong<n; dong++)//quet tu dong thu 1 den dong cuoi cung
for(dong1=n-1; dong1>=dong; dong1--)//quet nguoc tu dong cuoi len dong hien tai
for(cot=dong; cot<n; cot++)//quet cac cot
if(dong1+cot==dong+(n-1))//dieu kien de dien so vao ma tran
a[dong1][cot]=b[k++];
}
void SapXepLenXuong2(int a[][spt], int b[], int n)
{
int dong, dong1, cot, k=0;
for(dong=0; dong<n; dong++)
{
if(dong%2==0)//dieu kien de sap cac cac so theo chieu huong len
for(dong1=dong; dong1>=0; dong1--)
for(cot=0; cot<=dong; cot++)
if(dong1+cot==dong)
a[dong1][cot]=b[k++];
if(dong%2==1)//dieu kien de sap cac so theu chieu huong xuong
for(dong1=0; dong1<=dong; dong1++)
for(cot=dong; cot>=0; cot--)
if(dong1+cot==dong)
a[dong1][cot]=b[k++];
}
for(dong=1; dong<n; dong++)
{
if((n%2==1&&dong%2==0)||(n%2==0&&dong%2==1))//dieu kien de sap cac so theo chieu huong len
for(dong1=n-1; dong1>=dong; dong1--)
for(cot=dong; cot<n; cot++)
if(dong1+cot==dong+(n-1))
a[dong1][cot]==b[k++];
if((n%2==0&&dong%2==0)||(n%2==1&&dong%2==1))//dieu kien de sap cac so theo chieu huong xuong
for(dong1=dong; dong1<n; dong1++)
for(cot=n-1; cot>=dong; cot--)
if(dong1+cot==dong+(n-1))
a[dong1][cot]=b[k++];
}
}
void main()
{
int a[spt][spt];
int b[spt*spt];
int n;
NhapMaTran(a, n);
XuatMaTran(a, n);
MaTranSangMang(a, b, n);
BubbleSort(b, n*n);
SapXepLenXuong1(a, b, n);
XuatMaTran(a, n);
SapXepLenXuong2(a, b, n);
XuatMaTran(a, n);
}