C++ 生成 8x8 随机数矩阵并交换数字位置
#include
using namespace std;
int main() { // 生成随机数种子 srand(time(nullptr));
// 生成 8x8 的随机数矩阵
int matrix[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
matrix[i][j] = rand() % 10;
cout << matrix[i][j] << ' ';
}
cout << endl;
}
// 输入两个坐标并交换对应位置的数字
int x1, y1, x2, y2;
cout << '请输入要交换的两个数字的坐标(格式:x1 y1 x2 y2):';
cin >> x1 >> y1 >> x2 >> y2;
int temp = matrix[x1][y1];
matrix[x1][y1] = matrix[x2][y2];
matrix[x2][y2] = temp;
// 输出交换后的矩阵
cout << '交换后的矩阵:' << endl;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
cout << matrix[i][j] << ' ';
}
cout << endl;
}
return 0;
}
原文地址: http://www.cveoy.top/t/topic/oTeu 著作权归作者所有。请勿转载和采集!