题目描述:输入三个自然数Nij1==N1==N输出在一个NN格的棋盘中行列均从1开始编号与格子1同行、同列、同一对角线的所有格子的位置。如:n=4i=2j=3表示了棋盘中的第二行第三列的格子如下图:第一列 第二列 第三列 第四列 第一行 23
#include <iostream>
#include <string>
using namespace std;
int main() {
int N, i, j;
cin >> N >> i >> j;
// 输出同一行格子位置
for (int col = 1; col <= N; col++) {
cout << "(" << i << "," << col << ")";
}
cout << endl;
// 输出同一列格子位置
for (int row = 1; row <= N; row++) {
cout << "(" << row << "," << j << ")";
}
cout << endl;
// 输出左上到右下对角线上的格子位置
int startRow = i - min(i - 1, j - 1);
int startCol = j - min(i - 1, j - 1);
int endRow = i + min(N - i, N - j);
int endCol = j + min(N - i, N - j);
for (int row = startRow, col = startCol; row <= endRow && col <= endCol; row++, col++) {
cout << "(" << row << "," << col << ")";
}
cout << endl;
// 输出左下到右上对角线上的格子位置
startRow = i + min(N - i, j - 1);
startCol = j - min(N - i, j - 1);
endRow = i - min(i - 1, N - j);
endCol = j + min(i - 1, N - j);
for (int row = startRow, col = startCol; row >= endRow && col <= endCol; row--, col++) {
cout << "(" << row << "," << col << ")";
}
cout << endl;
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/inqF 著作权归作者所有。请勿转载和采集!