#include <stdlib.h>
#include <math.h>
#include <conio.h>
#define MAX_INDEX 100
void swap(int *a,int *b)
{
int t
t=*a
*a=*b
*b=t
}
/*快速排序算法*/
void QuickSort(int a[], int l, int r)
{
int i=l/*从左至右的游标*/
int j=r + 1/*从右到左的游标*/
int pivot=a[l]
if (l >= r) return
/*把左侧>= pivot的元素与右侧<= pivot 的元素进行交换*/
while (1)
{
do
{/*在左侧寻找>= pivot 的元素*/
i = i + 1
} while (a[i] <pivot)
do
{/*在右侧寻找<= pivot 的元素*/
j = j - 1
} while (a[j] >pivot)
if (i >= j) break/仔氏*未发现交换对象*/
swap(&a[i],&a[j])
}
/*设置p i v o t*/伍铅
a[l] = a[j]
a[j] = pivot
QuickSort(a, l, j-1)/*对左段排序*/
QuickSort(a, j+1, r)/*对右段排序*/
}
void Huanf(int Array[][MAX_INDEX],int n)
{
int i,j
int a,b,m
int tempArray1[MAX_INDEX]
int tempArray2[MAX_INDEX]
a=n/2
b=a+1
m=n%4
switch(m)
{
case 0:
case 2:
/*穿心腔戚好对调*/
for(i=0i<ni++)
for(j=0j<n/2j++)
{
if(i<n/2)
{
if(i%2==1&&Array[i][j]%2==0)/*偶行换偶*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
else if(i%2==0&&Array[i][j]%2==1)/*奇行换奇*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
}
else
{
if(i%2==1&&Array[i][j]%2==1)/*偶行换奇*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
else if(i%2==0&&Array[i][j]%2==0)/*奇行换偶*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
}
}
/*End穿心对调*/
if(m==2)
{
for(i=0i<n/2i++)
{
if((i!=0)&&(i!=a-1)&&(i!=b-1)&&(i!=n-1))
{
swap(&Array[i][a-1],&Array[n-1-i][a-1])
swap(&Array[b-1][i],&Array[b-1][n-1-i])
}
}
swap(&Array[0][a-1],&Array[0][b-1])
swap(&Array[a-1][0],&Array[b-1][0])
swap(&Array[2][0],&Array[2][n-1])
swap(&Array[0][2],&Array[n-1][2])
}
break
case 1:
case 3:
/*穿心对调*/
for(i=0i<ni++)
for(j=0j<n/2j++)
{
if(i<n/2)
{
if(i%2==1&&Array[i][j]%2==0) /*偶行换偶*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
else if(i%2==0&&Array[i][j]%2==0)/*奇行换奇*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
}
else if(i>n/2)
{
if(i%2==1&&Array[i][j]%2==0)/*偶行换偶*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
else if(i%2==0&&Array[i][j]%2==0)/*奇行换奇*/
{
swap(&Array[i][j],&Array[n-1-i][n-1-j])
}
}
}
/*End穿心对调*/
/*重排米字*/
for(i=0i<ni++)
{
tempArray1[i]=Array[i][i]
tempArray2[i]=Array[a][i]
}
QuickSort(tempArray1,0,n-1)
QuickSort(tempArray2,0,n-1)
for(i=0i<ni++)
{
Array[i][i]=tempArray2[i]
Array[a][i]=tempArray1[i]
}
for(i=0i<ni++)
{
tempArray1[i]=Array[i][n-1-i]
tempArray2[i]=Array[i][a]
}
QuickSort(tempArray1,0,n-1)
QuickSort(tempArray2,0,n-1)
for(i=0i<ni++)
{
Array[i][n-1-i]=tempArray2[i]
Array[i][a]=tempArray1[i]
}
/*End重排米字*/
if(m==3)
{
for(i=0i<n/2i++)
{
if((i!=a-1)&&(i!=b-1)&&(i!=a+1))
{
swap(&Array[i][a-1],&Array[n-1-i][a-1])
swap(&Array[a-1][i],&Array[a-1][n-1-i])
}
}
swap(&Array[a-1][a-1],&Array[a+1][a+1])
swap(&Array[a-1][b-1],&Array[a+1][b-1])
}
break
default:
break
}
return
}
void main()
{
int Ne[MAX_INDEX][MAX_INDEX]
int i,j,n
while(1)
{
printf("Please Input N (0 quit): ")
scanf("%d",&n)
if(n==0)
break
/*数组赋初值*/
for(i=0i<ni++)
for(j=0j<nj++)
Ne[i][j]=i*n+(j+1)
Huanf(Ne,n)
for(i=0i<ni++)
for(j=0j<nj++)
{
printf("%-4d",Ne[i][j])
if(j==n-1)
printf("\n\n")
}
printf("\n\n")
getch()
}
}
//给你看看一个: //存在三种情况下的幻方 #include <stdio.h>#include <stdlib.h>#define M 400 //------------------------------------- void _odd(int N){ //N=2n+1 int i, j, keyint square[M+1][M+1] = {0}i = 0j = (N+1) / 2for(key = 1key <= N*Nkey++) { if((key % N) == 1) i++else { i--j++} if(i == 0) i = Nif(j >N) j = 1square[i][j] = key} for(i = 1i <= Ni++) { for(j = 1j <= Nj++) printf("搏滚%2d ", square[i][j])printf("\n")} } //--------------------------------------------//4n void _Second(int N) { //4N int i, jint square[M+1][M+1] = {0}for(j = 1j <= Nj++) { for(i = 1i <= Ni++){ if(j % 4 == i % 4 || (j % 4 + i % 4) == 1) square[i][j] = (N+1-i) * N -j + 1else square[i][j] = (i - 1) * N + j} } for(i = 1i <= Ni++) { for(j = 1j <= Nj++) printf("%2d ", square[i][j])printf("\n")} } //------------------------//2(2n+1) #define SWAP(x,y) {int tt = xx = yy = t} void magic_o(int square[][M], int n) { int count, row, columnrow = 0column = n / 2for(count = 1count <= n*ncount++) { square[row][column] = count// 填A square[row+n][column+n] = count + n*n// 填B square[row][column+n] = count + 2*n*n// 填C square[row+n][column] = count + 3*n*n// 填D if(count % n == 0) row++else { row = (row == 0) ? n - 1 : row - 1 column = (column == n-1) ? 0 : column + 1} } } void exchange(int x[][M], int n) { int i, jint m = n / 4int m1 = m - 1for(i = 0i <n/2i++) { if(i != m) { for(j = 0j <mj++) // 處理規則 1 SWAP(x[i][j], x[n/2+i][j])for(j = 0j <m1j++) // 處理衫银腔規或衫則 2 SWAP(x[i][n-1-j], x[n/2+i][n-1-j])} else { // 處理規則 3 for(j = 1j <= mj++) SWAP(x[m][j], x[n/2+m][j])for(j = 0j <m1j++) SWAP(x[m][n-1-j], x[n/2+m][n-1-j])} } } void _Third(int N) { //2(2N+1) int square[M][M] = {0}int i, jmagic_o(square, N/2)exchange(square, N)for(i = 0i <Ni++) { for(j = 0j <Nj++) printf("%2d ", square[i][j])printf("\n")} } //-------------------- int main() { int Nwhile(scanf("%d",&N)!=EOF&&N>2) { if (N%2!=0) { _odd(N)} else if (N%4==0) { _Second(N)} else _Third(N)} return 0}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)