AtCoder Beginner Contest 170 A, B, C up to ruby

A problem

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 problem

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"

Forgetting when one is 0, I turned the loop from 1 and once wa

C problem

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

If the initial value of y is turned without the maximum value of p + 1, wa is not noticed at 1 1 1 and cannot be solved in time. Is the difference increased by 1 from 0 and included in the array rather than implementing it as written? It's simpler as a code to check.

Recommended Posts

AtCoder Beginner Contest 170 A, B, C up to ruby
AtCoder Beginner Contest 169 A, B, C with ruby
AtCoder Beginner Contest 167 C Problem (Java)
Solving with Ruby AtCoder ACL Beginner Contest C Union Find (DSU)
AtCoder Beginner Contest 168
diverta 2019 Programming Contest A & B & C & D
A memorandum to clean up the code Ruby
ABC --013-A & B & C
ABC --023 --A & B & C
ABC --036-A & B & C
ABC --010 --A & B & C
ABC --015 --A & B & C
ABC --128 --A & B & C
ABC --012-A & B & C
ABC --054 --A & B & C
ABC --017 --A & B & C
ABC --029- A & B & C
ABC --022 --A & B & C
ABC --019 --A & B & C
ABC --020 --A & B & C
ABC --030- A & B & C
ABC --127 --A & B & C
ABC --007 --A & B & C
ABC --132- A & B & C
ABC --026 --A & B & C
ABC --014- A & B & C
ABC --016 --A & B & C
ABC --011-A & B & C
ABC --031 --A & B & C
ABC --021 --A & B & C
ABC --025 --A & B & C
ABC --024 --A & B & C
ABC --027 --A & B & C
ABC --080- A & B & C
AtCoder Beginner Contest 182 Participation Article
ABC --129- A & B & C & D
ABC --133- A & B & C & D
ABC --122 --A & B & C & D
AtCoder Beginner Contest 132 D Problem
ABC --125- A & B & C & D
ABC --130- A & B & C & D
ABC --126 --A & B & C & D
Learning Ruby with AtCoder 13 How to make a two-dimensional array
AtCoder dwango Programming Contest B in Ruby, Perl and Java
Solve AtCoder Beginner Contest 151 in java
Solve AtCoder Beginner Contest 150 in java
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Solve AtCoder Beginner Contest 153 in java
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Solve AtCoder Beginner Contest 175 in java
ABC --131- A & B & C & D & E
Solve AtCoder Beginner Contest 160 in java
Solve AtCoder Beginner Contest 152 in java
Solve AtCoder Beginner Contest 156 in java
Minimal steps to set up a Ruby environment with rbenv on Ubuntu 20.04
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
Deploy to Heroku [Ruby on Rails] Beginner
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
AtCoder ABC 169 C Floating Point Fits in Ruby
[Ruby] I want to do a method jump!