配列活用メニュー
Bランク獲得ストーリーに含まれているメニューなので、やや難しめの立ち位置のようです。
STEP: 1 指定の要素のカウント
n, k = gets.split.map(&:to_i)
a = []
n.times { a << gets.to_i }
puts a.count(k)
STEP: 2 全ての要素の和
n = gets.to_i
a = []
n.times { a << gets.to_i }
puts a.sum
STEP: 3 配列の最大値
n = gets.to_i
a = []
n.times { a << gets.to_i }
puts a.max
STEP: 4 配列の最小値
n = gets.to_i
a = []
n.times { a << gets.to_i }
puts a.min
STEP: 5 指定要素があるかの判定
n, k = gets.split.map(&:to_i)
a = []
n.times { a << gets.to_i }
puts a.include?(k) ? "Yes" : "No"
STEP: 6 指定要素の先頭位置
n, k = gets.split.map(&:to_i)
a = []
n.times { a << gets.to_i }
puts a.include?(k) ? a.index(k) + 1 : -1
STEP: 7 要素の種類数
n = gets.to_i
a = []
n.times { a << gets.to_i }
puts a.uniq.length
FINAL問題 【配列を参照する操作】全ての要素に対する操作
n, k = gets.split.map(&:to_i)
a = []
n.times { a << gets.to_i + k}
puts a
STEP: 1 配列の順序の反転
n = gets.to_i
a = []
n.times { a << gets.to_i }
puts a.reverse
STEP: 2 変数の入れ替え
x, y = gets.split.map(&:to_i)
x, y = y, x
puts "#{x} #{y}"
STEP: 3 指定要素の入れ替え
n = gets.to_i
a = []
n.times { a << gets.to_i }
x, y = gets.split.map(&:to_i)
a[x - 1], a[y - 1] = a[y - 1], a[x - 1]
puts a
STEP: 4 末尾への要素の追加
n = gets.to_i
a = []
n.times { a << gets.to_i }
b = gets.to_i
a << b
puts a
STEP: 5 指定位置への要素の追加
n = gets.to_i
a = []
n.times { a << gets.to_i }
m, b = gets.split.map(&:to_i)
a.insert(m, b)
puts a
STEP: 6 指定要素の削除
n = gets.to_i
a = []
n.times { a << gets.to_i }
m = gets.to_i
a.delete_at(m - 1)
puts a
STEP: 7 九九表
(1..9).each do |i|
kuku = []
(1..9).each do |j|
kuku << i * j
end
puts kuku.join(' ')
end
STEP: 8 全ての要素を用いた処理
n = gets.to_i
a = n.times.map { gets.to_i }
(1...n).each do |i|
(0...i).each do |j|
puts a[i] * a[j]
end
end
配列aの作成を1行にしてみました。
STEP: 9 配列のサイズの変更
n, m = gets.split.map(&:to_i)
a = n.times.map { gets.to_i }
b = [0] * m
m.times do |i|
if a[i]
b[i] = a[i]
end
end
puts b
STEP: 10 重複要素の削除
n = gets.to_i
a = n.times.map { gets.to_i }
puts a.uniq
FINAL問題 【配列への副作用を伴う操作】条件を満たす要素のみの配列作成
n, k = gets.split.map(&:to_i)
a = n.times.map { gets.to_i }
b = []
n.times do |i|
if a[i] >= k
b << a[i]
end
end
puts b
STEP: 1 傾斜配点
n = gets.to_i
m = gets.split.map(&:to_i)
a = n.times.map { gets.split.map(&:to_i) }
ans = 0
n.times do |i|
result = 0
5.times do |j|
result += a[i][j] * m[j]
end
if ans < result
ans = result
end
end
puts ans
STEP: 2 内定
n, k, m = gets.split.map(&:to_i)
a = n.times.map { gets.to_i }
count = 0
n.times do |i|
if a[i] > k
count += 1
end
end
count -= m
if count <= 0
puts 0
else
puts count
end
STEP: 3 queue (9) 係
n = gets.to_i
a = []
n.times do
que = gets.chomp
if que == "out"
a.shift
else
q, m = que.split(' ')
a << m
end
end
puts a
STEP: 4 二人三脚
n = gets.to_i
a = n.times.map { gets.to_i }
a.sort!
ans = []
result = 1000
(n - 1).times do |i|
if a[i + 1] - a[i] < result
ans = [a[i], a[i + 1]]
result = a[i + 1] - a[i]
end
end
puts ans
身長差は1000で初期化しています。こういうのはクセで0とかにしがちですが、今回は数字を小さくしていきますから大きな数字で初期化が必要ですね。
STEP: 5 場所取り
n, k, f = gets.split.map(&:to_i)
a = k.times.map { gets.to_i }
a.shift(f)
puts a.uniq
STEP: 6 ボウリング
a = 4.times.map { gets.split.map(&:to_i) }
a.reverse!
(1..3).each { |i| a[i].reverse! }
a.flatten!
puts a.index(1) + 1
puts a.count(1)
一旦二次元配列でピンの情報を受け取り、ピンの番号順になるようreverse!で加工して、flatten!で配列に変更、最後にindexとcountで解答。
FINAL問題 【配列に対しての複雑な処理】集団行動
n, q = gets.split.map(&:to_i)
group = (1..n).to_a
q.times do
s = gets.chomp.split
if s[0] == "swap"
a, b = s[1].to_i, s[2].to_i
group[a - 1], group[b - 1] = group[b - 1], group[a - 1]
elsif s[0] == "reverse"
group.reverse!
else
c = s[1].to_i
group = group[0..c - 1]
end
end
puts group
resize Cでは先頭からCまでの配列を代入しているだけです。