AtCoder ABC 169 C Floating Point Fits in Ruby

Introduction

This theme

AtCoder Beginner Contets C - Multiplication 3 Difficulty: 536

This theme, error due to floating point

wa1.rb


a, b = gets.split.map(&:to_f)
puts (a * b).floor

Since it is truncated, floor is used, but WA. WA2

wa2.rb


a, b = gets.split.map(&:to_f)
puts ((a.to_i * (b * 100)) / 100).floor

Strategy to divide by 100 after100 times, failure. WA3

wa3.rb


a, b = gets.split.map(&:to_f)
puts ((a.to_i * (b * 100)) / 100).to_i

Try changing floor to to_i at the request of God. WA4

wa4.rb


a, b = gets.chomp.split
puts ((a.to_i * (b.to_f * 100).to_i) / 100)

A strategy to convert the reception from standard input into a character string. AC

ac.rb


a, b = gets.chomp.split
puts ((a.to_i * (b.gsub!('.', '').to_i).to_i) / 100)

A strategy to give up to_f by100 timesand delete the characters with the decimal point .. $ \ huge {success} $ </ font>

I don't know what was good and what was poor, but I was tired anyway.

Summary

  • Solved ABC 169 C
  • Not familiar with Ruby

** Addition ** Good articles have been posted

Recommended Posts

AtCoder ABC 169 C Floating Point Fits in Ruby
Solving in Ruby, Perl and Java AtCoder ABC 113 C Reference
atcoder ABC113 C problem
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
atcoder ABC115 C problem
Solving with Ruby, Perl and Java AtCoder ABC 128 C
ABC177 --solving E in Ruby
Microbenchmark for integer power of floating point numbers in Ruby
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
Solve with Ruby AtCoder ABC177 D UnionFind
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 2) Dynamic programming
AtCoder Beginner Contest 169 A, B, C with ruby
I made a Ruby extension library in C
Solving with Ruby and Crystal AtCoder ABC 129 D
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Class in Ruby
Heavy in Ruby! ??
Solving with Ruby and Java AtCoder ABC129 D 2D array
AtCoder Beginner Contest 170 A, B, C up to ruby