You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.5 KiB

2 years ago
#include <iostream>
#define SIZE 3 // SIZE来控制数组宽度
using namespace std;
const int S = 20; //数组的容器大小
int a[S][S];
//一个输出函数
void display() {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
// https://blog.csdn.net/BppleMan/article/details/50456645?locationNum=2&fps=1
// 关于45度遍历二维数组的一点记录
int main(int argc, const char *argv[]) {
int x, y, count = 1;
/*
1 3 6
2 5 8
4 7 9
*/
// 2 SIZE 1就是扫描行的最大行数
for (y = 0; y < 2 * SIZE - 1; y++) {
for (x = 0; x < SIZE; x++) {
//控制纵坐标的范围是0SIZE
if (y - x >= 0 && y - x < SIZE)
//通过找规律的到y与x的关系以此确定纵坐标
a[y - x][x] = count++;
}
}
display();
/*
1 2 4
3 5 7
6 8 9
*/
count = 1;
for (y = 0; y < 2 * SIZE - 1; y++) {
for (x = 0; x < SIZE; x++) {
if (y - x >= 0 && y - x < SIZE)
a[x][y - x] = count++;
}
}
display();
/*
4 2 1
7 5 3
9 8 6
*/
count = 1;
for (y = 0; y < 2 * SIZE - 1; y++) {
for (x = 0; x < SIZE; x++) {
if (x + y - (SIZE - 1) >= 0 && x + y - (SIZE - 1) < SIZE)
a[x + y - (SIZE - 1)][x] = count++;
}
}
display();
/*
6 3 1
8 5 2
9 7 4
*/
count = 1;
for (y = 0; y < 2 * SIZE - 1; y++) {
for (x = SIZE - 1; x >= 0; x--) {
if (x + y - (SIZE - 1) >= 0 && x + y - (SIZE - 1) < SIZE)
a[x + y - (SIZE - 1)][x] = count++;
}
}
display();
/*
6 8 9
3 5 7
1 2 4
*/
count = 1;
for (y = 0; y < 2 * SIZE - 1; y++) {
for (x = SIZE - 1; x >= 0; x--) {
if (x + y - (SIZE - 1) >= 0 && x + y - (SIZE - 1) < SIZE)
a[x][x + y - (SIZE - 1)] = count++;
}
}
display();
/*
4 7 9
2 5 8
1 3 6
*/
count = 1;
for (y = 0; y < 2 * SIZE - 1; y++) {
for (x = 0; x < SIZE; x++) {
if (x + y - (SIZE - 1) >= 0 && x + y - (SIZE - 1) < SIZE)
a[x][x + y - (SIZE - 1)] = count++;
}
}
display();
return 0;
}