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