如何以相反的顺序打印元素数组,而不仅仅是单个数字,还有多位数字.
[2, 5, 6 7]
Run Code Online (Sandbox Code Playgroud)
它应该以相反的顺序打印数组元素,方法是7 6 5 2按每个数字的空格.
我已经为此编写了代码.
puts "Enter the array elements"
arr = gets.strip
arr = arr.split(' ').map(&:to_i)
x = arr.reverse_each {|f| }
z = x.join(" ")
print z.reverse
Run Code Online (Sandbox Code Playgroud)
单位数字很酷,如何反转用户输入给出的输入数组中的多位数字,如:
[45, 76, 87 ] # this should reverse the array as `87 76 45`
[556, 674, 878 ] # this should reverse the array as `878 674 556`
[8797, 7347, 9374 ] # this should reverse the array as `9374 7374 8797`
Run Code Online (Sandbox Code Playgroud)
如果你喜欢单行:
gets.strip.split(' ').reverse.join(' ')
Run Code Online (Sandbox Code Playgroud)
这将获取输入1 2 3 45 678 9并将其转换为"9 678 45 3 2 1"