0
0

paizaラーニング解答: 標準入力サンプル問題セット[Ruby]

Last updated at Posted at 2024-01-11

標準入力サンプル問題セット

1つのデータの入力

s = gets.chomp
puts s

sはstring(文字列)の略で、とりあえず文字列を受け取るときによく使います。

1行のデータの入力

s = gets.chomp
puts s

3行のデータの入力

3.times do
    s = gets.chomp
    puts s
end

N行のデータの入力

n = gets.to_i
n.times do
    s = gets.chomp
    puts s
end

nはnumber(番号)の略で、とりあえず数字を受け取るときによく使います。

3つのデータの入力

a = gets.chomp.split
puts a

aはarray(配列)の略で、とりあえず配列を受け取るときによく使います。
splitで半角スペース毎に分け、配列に代入しています。

N個のデータの入力

n = gets.to_i
a = gets.chomp.split
puts a

nは使わなくてもOK。

カンマ区切りの3つのデータの入力

a = gets.chomp.split(',')
puts a

カンマ区切りのN個のデータの入力

n = gets.to_i
a = gets.chomp.split(',')
puts a
0
0
0

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
0
0