Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function

Introduction

AtCoder Beginner Contest 165 was held. I got into that D problem a little, so I would like to review ~~ revenge ~~.

This theme

AtCoder Beginner Contest 165 D - Floor Function Difficulty: 505

This theme, floor function Ruby Check input example 1 with Excel teacher. 20200503.png You can see that the cycle is exactly ** 7 **. If you think about it carefully, it is because it is divided by ** 7 **, so if n is b -1 or more, the maximum value that the function f can take, and if it is less than, n is a function (because it is a simple increase). The value assigned to f is the answer.

ruby.rb


a, b, n = gets.split.map(&:to_f)
if n >= b - 1
  puts (a - a / b).floor
else
  puts ((a * n / b).floor - a * (n / b).floor).floor
end

f.rb


a, b, n = gets.split.map(&:to_f)

I coded gets.split.map (&: to_i) as usual, so I was impatient because the calculation did not match.

python.py


a, b, n = map(int, input().split())
if n >= b - 1:
    print(int(a - a / b))
else:
    print(int(a * n / b) - a * int(n / b))

perl.pl


chomp (my ($a, $b, $n) = split / /, <STDIN>);
if ($n >= $b - 1) {
  print int($a - $a / $b), "\n";
} else {
  print (int($a * $n / $b) - $a * int($n / $b)), "\n";
}

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        Double b = Double.parseDouble(sc.next());
        Double n = Double.parseDouble(sc.next());
        sc.close();
        if (n >= b - 1) {
            System.out.println((int) (a - a / b));
        } else {
            System.out.println((int) (a * n / b) - a * (int) (n / b));
        }
    }
}
Ruby Python Perl Java
Code length 138 Byte 129 Byte 169 Byte 517 Byte
Execution time 53 ms 23 ms 2 ms 102 ms
memory 14204 KB 9016 KB 4788 KB 35648 KB

Summary

Recommended Posts

Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solve AtCoder ABC168 with python (A ~ D)
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby AtCoder ABC110 C String Manipulation
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
AtCoder ARC080 D simulation solved in Ruby and Python
Solve AtCoder ABC166 with python
AtCoder ABC 182 Python (A ~ D)
Solve AtCoder ABC 186 with Python
AtCoder ABC155 Problem D Pairs Review Note 2 NumPy and Python
AtCoder ABC168 A case expression solved in Ruby and Python
Scraping with Node, Ruby and Python
Solve ABC166 A ~ D with Python
Solved AtCoder ABC 114 C-755 with Python3
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
Learn Python! Comparison with Java (basic function)
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
MessagePack-Try to link Java and Python with RPC
AtCoder ABC 174 Python
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
AtCoder ABC187 Python
I tried function synthesis and curry with python
AtCoder ABC188 Python
Solving the Lorenz 96 model with Julia and Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
AtCoder ABC 175 Python
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)