AtCoder Beginner Contest 169 A, B, C with ruby

4th. Since I solved the C problem last time, I licked it that I could do it this time, and I stumbled upon the basics of computer engineering.

A problem

abc169a.rb


ab = gets.chomp.split(" ").map!{|item| item.to_i}
puts ab[0] * ab[1]

Just write.

B problem

abc169b.rb


n = gets.chomp.to_i
a = gets.chomp.split(" ").map!{|item| item.to_i}
num = 1
ans = 1
th = 10**18
a.sort!{|a,b| (-1)*(a <=> b)}
if a[n - 1] == 0
    puts 0
    exit
end
for num in 1..n do
    ans = ans * a[num-1]
    num = num + 1
    if ans > th
        puts "-1"
        exit
    end
end
puts ans

It's an integer multiplication, but in order

  1. If there is even one 0, 0
  2. If it exceeds 10 ^ 18, immediately return -1 and finish.
  3. Returns the product of all the elements Correct answer. With c, you need to check 2 carefully, but with ruby, you don't have to worry because it is an integer type and multiple-precision integers are used as needed. I wrote that it sorts in reverse order so that it gets caught in the check of 2 quickly, and checks whether the last element is 0 first, but a.include? (0) or a.min is enough. Don't forget to add! To sort destructively.

C problem

abc169c.rb


ab = gets.chomp.split(" ")
a=ab[0].to_i
b=((ab[1].to_s).delete(".")).to_i
ans = a*b/100
puts ans

Multiply b by 100, calculate it as an integer with to_s, and divide it by 100. When I did it, I stepped on the pattern of repeating decimals in binary notation. Treat b as an integer and to_s is easy after removing the decimal point. I can't pass this either. I will devote myself.

Recommended Posts

AtCoder Beginner Contest 169 A, B, C with ruby
AtCoder Beginner Contest 170 A, B, C up to ruby
Solving with Ruby AtCoder ACL Beginner Contest C Union Find (DSU)
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
AtCoder Beginner Contest 167 C Problem (Java)
AtCoder Beginner Contest 168
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]
diverta 2019 Programming Contest A & B & C & D
ABC --023 --A & B & C
ABC --036-A & B & C
ABC --010 --A & B & C
ABC --028 --A & B & C
ABC --015 --A & B & C
ABC --128 --A & B & C
ABC --012-A & B & C
ABC --018 --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
Solving with Ruby, Perl and Java AtCoder ABC 128 C
AtCoder Beginner Contest 182 Participation Article
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
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Solve AtCoder Beginner Contest 151 in java
ABC --134- A & B & C & D & E
Solve AtCoder Beginner Contest 150 in java
Solve AtCoder Beginner Contest 153 in 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
Make a typing game with ruby
Solving with Ruby AtCoder 1st Algorithm Practical Test A Exception Handling
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
Solve with Ruby AtCoder ABC177 D UnionFind
[Rails6] Create a new app with Rails [Beginner]