0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニング解答: 線形探索メニュー[Ruby]

Posted at

線形探索メニュー

線形探索についての詳しい解説は下記の講座で説明されています。

STEP: 1 指定された値の個数

n = gets.to_i
a = gets.split.map(&:to_i)
k = gets.to_i
puts a.count(k)

STEP: 2 指定された値の位置 1

n = gets.to_i
a = gets.split.map(&:to_i)
k = gets.to_i
if a.include?(k)
    puts a.index(k) + 1
else
    puts 0
end

STEP: 3 指定された値の位置 2

n = gets.to_i
a = gets.split.map(&:to_i)
k = gets.to_i
ans = 0
(n - 1).downto(0) do |i|
    if a[i] == k
        ans = i + 1
        break
    end
end
puts ans

FINAL問題 【指定された値の探索】指定された値の位置 3

n = gets.to_i
a = gets.split.map(&:to_i)
k = gets.to_i
(0...n).each do |i|
    if a[i] == k
        puts i + 1
    end
end

STEP: 1 2変数の最大最小

a = gets.split.map(&:to_i)
puts "#{a.max} #{a.min}"

STEP: 2 10変数の最大最小

a = gets.split.map(&:to_i)
puts "#{a.max} #{a.min}"

FINAL問題 【最大最小】n 変数の最大最小

n = gets.to_i
a = gets.split.map(&:to_i)
puts "#{a.max} #{a.min}"

STEP: 1 偶数の探索

n = gets.to_i
a = gets.split.map(&:to_i)
(0...n).each do |i|
    if a[i].even?
        puts i + 1
        break
    end
end

STEP: 2 奇数の探索

n = gets.to_i
a = gets.split.map(&:to_i)
(n - 1).downto(0) do |i|
    if a[i].odd?
        puts i + 1
        break
    end
end

STEP: 3 条件付き最小値

n = gets.to_i
a = gets.split.map(&:to_i)
k = gets.to_i
ans = 1000
a.each do |i|
    if k <= i
        ans = [i, ans].min
    end
end
puts ans

STEP: 4 条件付き最大値

n = gets.to_i
a = gets.split.map(&:to_i)
k = gets.to_i
ans = -1000
a.each do |i|
    if k >= i
        ans = [i, ans].max
    end
end
puts ans

STEP: 5 点と点の距離

n = gets.to_i
a = n.times.map { gets.split.map(&:to_i) }
k = gets.to_i
xn, yn = a[-1]
count = 0
n.times do |i|
    x, y = a[i][0], a[i][1]
    if (x - xn).abs + (y - yn).abs <= k
        count += 1
    end
end
puts count

問題文だとわかりにくいですが、点nとは入力される値の中の最後のxとyのことです。

STEP: 6 長方形に含まれる点

n = gets.to_i
a = n.times.map { gets.split.map(&:to_i) }
xs, xt = gets.split.map(&:to_i)
ys, yt = gets.split.map(&:to_i)

count = 0
n.times do |i|
    x, y = a[i][0], a[i][1]
    if xs <= x && x <= xt && ys <= y && y <= yt
        count += 1
    end
end
puts count

STEP: 7 成績優秀者の列挙 1

n = gets.to_i
a = n.times.map { gets.chomp.split }
k = gets.to_i
n.times do |i|
    s, t = a[i][0], a[i][1].to_i
    if k <= t
        puts s
    end
end

FINAL問題 【特殊な探索】 成績優秀者の列挙 2

n = gets.to_i
a = n.times.map { gets.chomp.split }
k, l = gets.split.map(&:to_i)
n.times do |i|
    s, t = a[i][0], a[i][1].to_i
    if k <= t && t <= l
        puts s
    end
end

STEP: 1 2番目に大きな値

n = gets.to_i
a = gets.split.map(&:to_i)
a.sort!
puts a[n - 2]

FINAL問題 【第 k 要素の探索】k番目に大きな値

n = gets.to_i
a = gets.split.map(&:to_i)
k = gets.to_i
puts a.sort[-k]
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?