我想生成四位数字(从0000
到9999
),看起来像ABCD
和AABC
。
例如,模式ABCD
将具有类似
0123
,0124
,0125
,0126
,0127
,0128
,0129
等等。
模式AABC如:
0012
,0013
,0014
,0015
,0016
等。
答案 0 :(得分:2)
(0..9).to_a.permutation(4).map{|a, b, c, d| [a, b, c, d].join}
(0..9).to_a.permutation(3).map{|a, b, c| [a, a, b, c].join}
或者
(0..9).to_a.permutation(4).map{|a, b, c, d| "#{a}#{b}#{c}#{d}"}
(0..9).to_a.permutation(3).map{|a, b, c| "#{a}#{a}#{b}#{c}"}