LoginSignup
0
0

More than 3 years have passed since last update.

AtCoder Beginner Contest 170 A,B,Cまでをrubyで

Posted at

A問題

abc170a.rb
xi = gets.chomp.split(" ").map!{|item| item.to_i}
for i in 1..5 do
    if xi[i-1] == 0
    print(i )
    end
end

B問題

abc170.rb
xi = gets.chomp.split(" ").map!{|item| item.to_i}

for f in 0..xi[0]
    if xi[1] == (f*2) + (xi[0]-f)*4
        puts "Yes"
        exit
    end
end
puts "No"

片方0のときを忘れてループを1から回してしまい一度wa

C問題

abc170.rb
x,n = gets.chomp.split(" ").map!{|item| item.to_i}
p = gets.chomp.split(" ").map!{|item| item.to_i}
p.sort!
if n == 0
    puts x
    exit
end

y = p.max+1
for i in 0..p.max+1 do
    if y > (x-i).abs
        if p.include?(i)
        else
        y = (x-i).abs
        a = i
        end
    end
end
puts a

yの初期値をpの最大値+1せずに回すと1 1 1の時waに気づけず、時間内に解けず。
文面通りに実装するより、差が0から1づつ増やしていき、配列中に含まれるか?を確認する方がコードとして単純ですね。

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