6.
矩阵中填数
.
当给出
N*N
的矩阵,要求用程序填入下列形式的数:
①
倒填,例如
N=5
②
蛇形填数
③
回转填数
┌─┬─┬─┬─┬─┐
┌─┬─┬─┬─┬─┐
┌─┬─┬─┬─┬─┐
│
25
│
24
│
23
│
22
│
21
│
│
1
│
3
│
4
│
10
│
11
│
│
1
│
16
│
15
│
14
│
13
│
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
│
20
│
19
│
18
│
17
│
16
│
│
2
│
5
│
9
│
12
│
19
│
│
2
│
17
│
24
│
23
│
12
│
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
│
15
│
14
│
13
│
12
│
11
│
│
6
│
8
│
13
│
18
│
20
│
│
3
│
18
│
25
│
22
│
11
│
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
│
10
│
9
│
8
│
7
│
6
│
│
7
│
14
│
17
│
21
│
24
│
│
4
│
19
│
20
│
21
│
10
│
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
├─┼─┼─┼─┼─┤
│
5
│
4
│
3
│
2
│
1
│
│
15
│
16
│
22
│
23
│
25
│
│
5
│
6
│
7
│
8
│
9
│
└─┴─┴─┴─┴─┘
└─┴─┴─┴─┴─┘
└─┴─┴─┴─┴─┘
[C#]
6.1
static void Main(string[] args)
{
int N,n;
static void Main(string[] args)
{
int N,n;
Console.WriteLine("N : ");
string sN = Console.ReadLine();
N = Convert.ToInt32(sN);
n = N * N;
string sN = Console.ReadLine();
N = Convert.ToInt32(sN);
n = N * N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
Console.Write("{0,4}", n--);//-4 左对齐 ,4 右对齐
Console.Write("/n");
}
Console.ReadLine();
}
{
for (int j = 0; j < N; j++)
Console.Write("{0,4}", n--);//-4 左对齐 ,4 右对齐
Console.Write("/n");
}
Console.ReadLine();
}
6.2
static void Main(string[] args)
{
int N;
int i, j, num,start,end;
j = 0;
num = 0;
static void Main(string[] args)
{
int N;
int i, j, num,start,end;
j = 0;
num = 0;
Console.WriteLine("N : ");
string sN = Console.ReadLine();
N = Convert.ToInt32(sN);
string sN = Console.ReadLine();
N = Convert.ToInt32(sN);
int[,] a = new int[N, N];
for (int sum = 0; sum < 2 * N - 1; sum++)
{
start = (sum < N) ? 0 : (sum - (N - 1));
//end = (sum < N) ? N : (sum - N);
end = (sum < N) ? sum : (N-1);
{
start = (sum < N) ? 0 : (sum - (N - 1));
//end = (sum < N) ? N : (sum - N);
end = (sum < N) ? sum : (N-1);
for (i = start; i <= end; i++)//是<= 不是〈
if (sum % 2 == 1)
a[i, sum - i] = ++num;
else
a[sum - i, i] = ++num;
if (sum % 2 == 1)
a[i, sum - i] = ++num;
else
a[sum - i, i] = ++num;
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
Console.Write("{0,4}", a[i, j]);//-4 左对齐 ,4 右对齐
Console.Write("/n");
}
Console.ReadLine();
}
{
for (j = 0; j < N; j++)
Console.Write("{0,4}", a[i, j]);//-4 左对齐 ,4 右对齐
Console.Write("/n");
}
Console.ReadLine();
}
6.3
static void Main(string[] args)
{
int N;
int i, j, num;
//i = 0;
j = 0; //Error 1 Use of unassigned local variable 'j'
num = 0;//Error 2
static void Main(string[] args)
{
int N;
int i, j, num;
//i = 0;
j = 0; //Error 1 Use of unassigned local variable 'j'
num = 0;//Error 2
Console.WriteLine("N : ");
string sN = Console.ReadLine();
N = Convert.ToInt32(sN);
string sN = Console.ReadLine();
N = Convert.ToInt32(sN);
int[,] a = new int[N, N];
int start = 0, end = N -1;
for (int t = 0; t <= N / 2; t++, start++, end--, i++, j++)
//理解 t <=N / 2; i++, j++
{
for(i = start; i < end; i++)
a[i, j] = ++num;
//Error 1 Use of unassigned local variable 'j'
//Error 2 Use of unassigned local variable 'num'
for(j = start; j < end; j++)
a[i,j] = ++num;
for(i = end; i > start; i--)
a[i,j] = ++num;
for(j = end; j > start; j--)
a[i,j] = ++num;
if(start == end)
a[start,end] = ++num;
}
int start = 0, end = N -1;
for (int t = 0; t <= N / 2; t++, start++, end--, i++, j++)
//理解 t <=N / 2; i++, j++
{
for(i = start; i < end; i++)
a[i, j] = ++num;
//Error 1 Use of unassigned local variable 'j'
//Error 2 Use of unassigned local variable 'num'
for(j = start; j < end; j++)
a[i,j] = ++num;
for(i = end; i > start; i--)
a[i,j] = ++num;
for(j = end; j > start; j--)
a[i,j] = ++num;
if(start == end)
a[start,end] = ++num;
}
for ( i = 0; i < N; i++)
{
for ( j = 0; j < N; j++)
Console.Write("{0,4}", a[i,j]);//-4 左对齐 ,4 右对齐
Console.Write("/n");
}
Console.ReadLine();
}
{
for ( j = 0; j < N; j++)
Console.Write("{0,4}", a[i,j]);//-4 左对齐 ,4 右对齐
Console.Write("/n");
}
Console.ReadLine();
}
[C++]
//6.1
#include "stdio.h"
void main()
{
int n, i, j;
scanf("%d", &n);
int t = n*n;
for(i = n; i > 0; i--)
{
for(j = n; j > 0; j--)
printf("%5d", t--);
printf("/n");
}
}
#include "stdio.h"
void main()
{
int n, i, j;
scanf("%d", &n);
int t = n*n;
for(i = n; i > 0; i--)
{
for(j = n; j > 0; j--)
printf("%5d", t--);
printf("/n");
}
}
// 6.2
#include "stdio.h"
#define N 15
void main()
{
int sum, j, t = 1, start, end;
static int a[N][N];
for(sum = 0; sum < 2*N -1; sum++)
{
start = (sum < N)?0:sum-(N-1);
end = (sum < N)?sum:(N-1);
for(j = start; j <= end; j++)
(sum%2)?(a[j][sum-j] = t++):(a[sum-j][j] = t++); //(sum%2)控制环绕方向
}
for(sum = 0; sum < N; sum++)
{
for(j = 0; j < N; j++)
printf("%5d", a[sum][j]);
printf("/n");
}
}
#include "stdio.h"
#define N 15
void main()
{
int sum, j, t = 1, start, end;
static int a[N][N];
for(sum = 0; sum < 2*N -1; sum++)
{
start = (sum < N)?0:sum-(N-1);
end = (sum < N)?sum:(N-1);
for(j = start; j <= end; j++)
(sum%2)?(a[j][sum-j] = t++):(a[sum-j][j] = t++); //(sum%2)控制环绕方向
}
for(sum = 0; sum < N; sum++)
{
for(j = 0; j < N; j++)
printf("%5d", a[sum][j]);
printf("/n");
}
}
// 6.3
#include <stdio.h>
#define elem a[i][j] //用于控制方向,若改为a[j][i],则方向相反
#define N 10
void main()
{
static int a[N][N], i, j, num;
int start = 0, end = N -1;
for(int t =0; t <= N/2; t++, start++, end--, i++, j++)
{
for(i = start; i < end; i++)
a[i][j] = ++num;
for(j = start; j < end; j++)
a[i][j] = ++num;
for(i = end; i > start; i--)
a[i][j] = ++num;
for(j = end; j > start; j--)
a[i][j] = ++num;
if(start == end)
a[start][end] = ++num;
}
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%4d", elem);
printf("/n");
}
}
#include <stdio.h>
#define elem a[i][j] //用于控制方向,若改为a[j][i],则方向相反
#define N 10
void main()
{
static int a[N][N], i, j, num;
int start = 0, end = N -1;
for(int t =0; t <= N/2; t++, start++, end--, i++, j++)
{
for(i = start; i < end; i++)
a[i][j] = ++num;
for(j = start; j < end; j++)
a[i][j] = ++num;
for(i = end; i > start; i--)
a[i][j] = ++num;
for(j = end; j > start; j--)
a[i][j] = ++num;
if(start == end)
a[start][end] = ++num;
}
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%4d", elem);
printf("/n");
}
}