c++ - 如何生成循环矩阵?

标签 c++ c matrix

我想用 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/

相关文章:

c - 使用 bool 表达式代替 if/else 或类似条件

Matlab triu函数转向量

r - 用 R 构造约束矩阵

c++ - 有没有办法在平均值中不包含负数,当输入负数时你如何终止程序?

c - 用于解析参数 GNU 风格的库?

c - clang 和 gcc 之间的 float 操作结果不一致

python - 在python中测试矩阵是否为半正定(PSD)时出错

C++ 串口通讯

c++ - 使用 inputMask 时不显示 TextFields 中的 placeholderText

C++ 将双参数解析为字符串?