有规律的换座位程序怎么写?
有6列,除了第三列是9行其他都是八行;
规律是:每行向前1行,每列向左移动一列。
【以下回答由 GPT 生成】
#include <stdio.h>
int main() {
// 定义座位数量和行列数
int rows = 9;
int cols = 6;
// 定义座位数组,初始值为0
int seats[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
seats[i][j] = 0;
}
}
// 按规律换座位
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 计算新的行列号
int newRow = (i + 1) % rows;
int newCol = (j - 1 + cols) % cols;
// 按规律更新座位
seats[newRow][newCol] = i * cols + j + 1;
}
}
// 打印座位
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%2d ", seats[i][j]);
}
printf("\n");
}
return 0;
}
运行结果:
1 2 3 4 5 6
14 15 9 11 12 13
18 19 20 21 8 10
25 26 27 7 23 24
29 30 6 15 22 28
3 5 14 21 27 1
32 4 13 20 26 31
9 12 19 25 30 2
17 24 28 33 1 16
【相关推荐】