产生大小为n的所有列表,使得每个元件是介于0和米(含) [英] Generate all lists of size n, such that each element is between 0 and m (inclusive)

查看:209
本文介绍了产生大小为n的所有列表,使得每个元件是介于0和米(含)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成大小的所有列表 N ,使得每个单元是介于0和 M (含)。

Generate all lists of size n, such that each element is between 0 and m (inclusive).

(M + 1)^ N 这样的名单。

推荐答案

有写一般情况下两种简单的方法。一个是从@didierc已有答案描述。另一种方法是递归。

There are two easy ways of writing the general case. One is described in the existing answer from @didierc. The alternative is recursion.

例如,想想一个方法,需要一个字符串参数:

For example, think about a method that takes a String as an argument:

if(input string is long enough)
  print or store it
else
  iterate over digit range
    recursive call with the digit appended to the string

这篇关于产生大小为n的所有列表,使得每个元件是介于0和米(含)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆