質問

QUESTION :

Input:
  4 # number of input 
  1
  2
  4
  3
Output:
  1
  2
  24
  6

CANNOT GET DESIRED OUTPUT

My code:
num  = Integer(gets.chomp)
k = []
for i in 1..num
k[i] = Integer(gets.chomp)
end

k.each do |w|
for i in 1..w
w.to_i = w*i
end
puts w 
end
役に立ちましたか?

解決 2

you can try like:

input = Integer(gets.chomp) 
ans = 1
for i in 1..input
 ans = ans*i
end
print(ans)

他のヒント

To get factorial of n

(1..n).inject :*

To take care of zero

(1..n).inject(1, :*)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top