LoginSignup
2
2

More than 1 year has passed since last update.

全ての行の長さが不定な2次元配列(Ruby)

Posted at

paiza問題集で下記の問題をRubyで実装しました。

自然数 N と N 個の要素の数列 M が与えられます。1 ≦ i ≦ N の各 i について、i 行目には以下の数列を出力してください。

  • 1 以上 M_i 以下のすべての自然数を昇順、半角スペース区切りで出力してください。
# 自分の得意な言語で
# Let's チャレンジ!!
num = gets.to_i 
data = gets 
array1 = data.split(' ')
count = array1.length 
for i in 0...count do
    num2 = array1[i]
    str1 = ""
    for j in 1..num2.to_i do 
        if j === num2.to_i 
            str1 = str1 + j.to_s
        else   
            str1 = str1 + j.to_s + " "
        end    
    end 
    puts str1 
end      
2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2