我想用 C 或 C++ 生成循环矩阵。
当 n = 3 时,如何生成下面的矩阵?
1 2 3
8 9 4
7 6 5
最佳答案
我以前做过几次...
伪代码:
min_x = 0;
min_y = 0;
max_x = X;
max_y = Y;
while(!all_fields_filled){
// move right -------------------------
for(i = min_x; i <= max_x; i++){
array[min_y][i] = fields_number;
fields_number++;
}
min_y++
// it is important to check that condition after each for
// (our total field number could be not divided by 4)
if(filled_fields == fields_amount) break;
// edn "move right" procedure -----------
// ETC. for move DOWN, next LEFT and UP
// remember to increase min_x/min_y and decrease max_y/max_y
}
关于c++ - 如何生成循环矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4154339/