Q1
你不需要被告知。想辦法。假設p(n)
是您可以安排(「置換」)具有不同數字的數字的數字的方式的數量。首先,假設n=1
。那麼很明顯,
p(1) = 1
現在假設n=2
。第一個數字可以之前或之後的第二位,所以:
p(2) = 2*p(1) = 2
現在讓n
是任意整數比2
更大。對於最後一個n-1
數字的每個排列,第一個數字可以插入n
的任意位置。例如,假設數字是1234
。最後三位數字的一種排列是423
。第一個數字1
可插入4
位置的任何一個:1423, 4123, 4213, 4231
。
因此:
p(n) = n*p(n-1)
看來:
p(n) = n!
我們可以很容易地證明,通過感應是正確的:
p(1) = 1
p(2) = 2*1 = 2!
p(n) = n*p(n-1)
= n*(n-1)!
= n!
所以:
p(3) = 3! = 6
p(4) = 4! = 4*3! = 4*6 = 24
現在你已經知道了這個公式,並知道爲什麼它是真的,你不會忘記它。
Q2
讓我們重寫方法與一些puts
補充:
def number_shuffle(number)
no_of_combinations = number.to_s.size == 3 ? 6 : 24
puts "no_of_combinations = #{no_of_combinations}"
digits = number.to_s.split(//)
puts "digits = #{digits}\n\n"
combinations = []
while true
a = digits.shuffle.join.to_i
puts "digits.shuffle.join.to_i = #{a}"
combinations << a
puts "combinations = #{combinations}"
b = combinations.uniq
puts " combinations.uniq = #{b}"
c = b.size
puts " combinations.uniq.size = #{c}"
puts " combinations.uniq.size==no_of_combos = #{c==no_of_combinations}"
break if c==no_of_combinations
end
combinations.uniq.sort
end
和嘗試:
number_shuffle(123)
no_of_combinations = 6
digits = ["1", "2", "3"]
digits.shuffle.join.to_i = 123
combinations = [123]
combinations.uniq = [123]
combinations.uniq.size = 1
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 321
combinations = [123, 321]
combinations.uniq = [123, 321]
combinations.uniq.size = 2
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 123
combinations = [123, 321, 123]
combinations.uniq = [123, 321]
combinations.uniq.size = 2
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 312
combinations = [123, 321, 123, 312]
combinations.uniq = [123, 321, 312]
combinations.uniq.size = 3
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 321
combinations = [123, 321, 123, 312, 321]
combinations.uniq = [123, 321, 312]
combinations.uniq.size = 3
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 132
combinations = [123, 321, 123, 312, 321, 132]
combinations.uniq = [123, 321, 312, 132]
combinations.uniq.size = 4
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 321
combinations = [123, 321, 123, 312, 321, 132, 321]
combinations.uniq = [123, 321, 312, 132]
combinations.uniq.size = 4
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 213
combinations = [123, 321, 123, 312, 321, 132, 321, 213]
combinations.uniq = [123, 321, 312, 132, 213]
combinations.uniq.size = 5
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 213
combinations = [123, 321, 123, 312, 321, 132, 321, 213, 213]
combinations.uniq = [123, 321, 312, 132, 213]
combinations.uniq.size = 5
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 123
combinations = [123, 321, 123, 312, 321, 132, 321, 213, 213, 123]
combinations.uniq = [123, 321, 312, 132, 213]
combinations.uniq.size = 5
combinations.uniq.size==no_of_combinations = false
digits.shuffle.join.to_i = 231
combinations = [123, 321, 123, 312, 321, 132, 321, 213, 213, 123, 231]
combinations.uniq = [123, 321, 312, 132, 213, 231]
combinations.uniq.size = 6
combinations.uniq.size==no_of_combinations = true
#=> [123, 132, 213, 231, 312, 321]
「號是遠非理想,因爲可以一遍又一遍地創建相同的組合。「絕對的,所以看起來OP提供的解決方案根本不是解決方案。這就是對教程的一個聲明。 : -/ – 2015-04-02 16:56:01
給予練習和方法名稱的教程 – steenslag 2015-04-02 18:46:01