/*
Name:
Copyright: goal00001111
Author:
Date: 14-11-08 09:25
Description: 蛇形矩阵的各种情形:共4种5类
*/
#include<iostream>
using namespace std;
const int MAX = 20;
int a[MAX][MAX];
void Fun_1(int n);
void Fun_2(int n);
void Fun_3(int n);
void Fun_4(int n);
void Fun_5(int n);
int main(void)
{
int n;
cout << "请输入矩阵大小:" << endl;
cin >> n;
Fun_1(n);
cout << endl;
Fun_2(n);
cout << endl;
Fun_3(n);
cout << endl;
Fun_4(n);
cout << endl;
Fun_5(n);
system("pause");
return 0;
}
void Fun_1(int n)
{
int s = 1;
for (int i=0; i<n; i++)
{
if (i % 2 == 0)
{
for (int j=0; j<n; j++)
a[i][j] = s++;
}
else
{
for (int j=n-1; j>=0; j--)
a[i][j] = s++;
}
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",a[i][j]);
cout << endl;
}
}
void Fun_2(int n)
{
int i, j, s;
i = j = 0;
s = 1;
a[i][j] = s++;
while (s <= n*n)
{
//下移
if (i < n-1)
i++;
else
j++;
a[i][j] = s++;
//右上移动
while (j < n-1 && i > 0)
a[--i][++j] = s++;
//右移
if (j < n-1)
j++;
else
i++;
a[i][j] = s++;
//左下移动
while (i < n-1 && j > 0)
a[++i][--j] = s++;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",a[i][j]);
cout << endl;
}
}
void Fun_3(int n)
{
int left, right, up, down;
left = up = 0;
right = down = n - 1;
int s = 1;
while (left <= right)
{
for (int i=left; i<=right; i++)//顺序横向
a[up][i] = s++;
up++; //下移一行
for (int i=up; i<=down; i++)//顺序纵向
a[i][right] = s++;
right--; //左移一列
for (int i=right; i>=left; i--)//顺序横向
a[down][i] = s++;
down--; //上移一行
for (int i=down; i>=up; i--)//顺序纵向
a[i][left] = s++;
left++;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",a[i][j]);
cout << endl;
}
}
void Fun_4(int n)
{
int left, right, up, down;
left = up = 0;
right = down = n - 1;
int s = 1;
while (left <= right)
{
for (int i=left; i<=right; i++)//顺序横向
a[up][i] = s++;
up++; //下移一行
for (int i=up; i<=down; i++)//顺序纵向
a[i][right] = s++;
right--; //左移一列
for (int i=right; i>=left; i--)//顺序横向
a[down][i] = s++;
down--; //上移一行
for (int i=down; i>=up; i--)//顺序纵向
a[i][left] = s++;
left++;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",n*n + 1 - a[i][j]);
cout << endl;
}
}
void Fun_5(int n)
{
for (int s=1; s<=n; s++)
{ //列
for (int i=s-1; i<n+n-s; i++)//s<n/2
a[i][s-1] = a[i][n+n-s-1] = s;
//行
for (int i=s-1; i<n+n-s; i++)//s<n/2
a[s-1][i] = a[n+n-s-1][i] = s;
}
for (int i=0; i<n+n-1; i++)
{
for (int j=0; j<n+n-1; j++)
printf("%3.0d ", a[i][j]);
cout << endl;
}
}
Name:
Copyright: goal00001111
Author:
Date: 14-11-08 09:25
Description: 蛇形矩阵的各种情形:共4种5类
*/
#include<iostream>
using namespace std;
const int MAX = 20;
int a[MAX][MAX];
void Fun_1(int n);
void Fun_2(int n);
void Fun_3(int n);
void Fun_4(int n);
void Fun_5(int n);
int main(void)
{
int n;
cout << "请输入矩阵大小:" << endl;
cin >> n;
Fun_1(n);
cout << endl;
Fun_2(n);
cout << endl;
Fun_3(n);
cout << endl;
Fun_4(n);
cout << endl;
Fun_5(n);
system("pause");
return 0;
}
void Fun_1(int n)
{
int s = 1;
for (int i=0; i<n; i++)
{
if (i % 2 == 0)
{
for (int j=0; j<n; j++)
a[i][j] = s++;
}
else
{
for (int j=n-1; j>=0; j--)
a[i][j] = s++;
}
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",a[i][j]);
cout << endl;
}
}
void Fun_2(int n)
{
int i, j, s;
i = j = 0;
s = 1;
a[i][j] = s++;
while (s <= n*n)
{
//下移
if (i < n-1)
i++;
else
j++;
a[i][j] = s++;
//右上移动
while (j < n-1 && i > 0)
a[--i][++j] = s++;
//右移
if (j < n-1)
j++;
else
i++;
a[i][j] = s++;
//左下移动
while (i < n-1 && j > 0)
a[++i][--j] = s++;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",a[i][j]);
cout << endl;
}
}
void Fun_3(int n)
{
int left, right, up, down;
left = up = 0;
right = down = n - 1;
int s = 1;
while (left <= right)
{
for (int i=left; i<=right; i++)//顺序横向
a[up][i] = s++;
up++; //下移一行
for (int i=up; i<=down; i++)//顺序纵向
a[i][right] = s++;
right--; //左移一列
for (int i=right; i>=left; i--)//顺序横向
a[down][i] = s++;
down--; //上移一行
for (int i=down; i>=up; i--)//顺序纵向
a[i][left] = s++;
left++;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",a[i][j]);
cout << endl;
}
}
void Fun_4(int n)
{
int left, right, up, down;
left = up = 0;
right = down = n - 1;
int s = 1;
while (left <= right)
{
for (int i=left; i<=right; i++)//顺序横向
a[up][i] = s++;
up++; //下移一行
for (int i=up; i<=down; i++)//顺序纵向
a[i][right] = s++;
right--; //左移一列
for (int i=right; i>=left; i--)//顺序横向
a[down][i] = s++;
down--; //上移一行
for (int i=down; i>=up; i--)//顺序纵向
a[i][left] = s++;
left++;
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
printf("%3.0d ",n*n + 1 - a[i][j]);
cout << endl;
}
}
void Fun_5(int n)
{
for (int s=1; s<=n; s++)
{ //列
for (int i=s-1; i<n+n-s; i++)//s<n/2
a[i][s-1] = a[i][n+n-s-1] = s;
//行
for (int i=s-1; i<n+n-s; i++)//s<n/2
a[s-1][i] = a[n+n-s-1][i] = s;
}
for (int i=0; i<n+n-1; i++)
{
for (int j=0; j<n+n-1; j++)
printf("%3.0d ", a[i][j]);
cout << endl;
}
}