以特定顺序生成以 3 位数字为基数的数字的所有数组表示的算法?

问题描述

我试图找出一种在编译时生成整数数组序列的方法(使用 constexpr 函数)。顺序是这样的:

// first all order 0 numbers
{0,0},// then all order 1 numbers (total sum of figures = 1)
{1,{0,1,1},// then all order 2 numbers (total sum of figures = 2) in no specific order
{1,{1,{2,2,2}
// then all order 3 numbers etc... each block in no specific order

目前我只能编写算法以另一种顺序获取这些数组

constexpr int M = 5; // maximum figure available,each figure will be from 0 to 5
constexpr int N = (M + 3) * (M + 2) / 2; // total number of arrays to be generated

constexpr std::array<std::array<int,3>,N> algorithm() {
  std::array<std::array<int,N> ans;
  int pos = 0;
  for (int i = 0; i <= M; ++i) {
    for (int j = 0; j <= M - i; ++j) {
      for (int k = 0; k <= M - i - j; ++k) {
        ans[pos] = std::array<int,3>{k,j,i};
        ++pos;
      }
    }
  }
  return ans;
}

这当然会以错误的顺序给出数组:

{0,{3,...

我知道这在编译时可能是不可能的,但也许有人知道此类问题的任何提示?感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...