AtCoder Beginner Contest 176 A Explanation of problem "Takoyaki" (Python3, C ++, Java)

This is Rute! Everyone AtCoder Beginner Contest 176 Thank you for your hard work! !! This time, it was the ABC quick-solving contest since ABC164 </ font> </ b>, which is A-problem, B-problem, and C-problem quick-solving. Probably, there were many people who said, "Complete ABC 3 and withdraw."

This time, I would like to explain the A, B, and C problems in three languages! !!

Links to each problem / explanation

A B C
This article! !! ABC 176 B ABC 176 C

Let's start with an explanation of the A problem </ b>! !!

Problem summary

The takoyaki machine can make up to $ X $ takoyaki at a time. The time it takes to make it is $ T $ regardless of the number.

Output how many minutes it takes to make $ N $ takoyaki.

Constraint

・ $ 1 \ leq N, X, T \ leq 1000 $ ・ All inputs are integers

Commentary

Solution 1 (using ceiling function)

The number of times the takoyaki machine is used to make $ N $ takoyaki is $ \ lceil {\ frac {N} {X}} \ rceil $.

(Here, $ \ lceil {A} \ rceil $ is called the ceiling function </ b> of A, and specifically means the minimum integer </ b> of $ A $ or more. Has.) Therefore, multiplying it by $ T $ is the time required to make $ N $ takoyaki, so you can output this! !!

Solution 2 (use of conditional branching)

Or, it seems good to consider the following conditional branching. -If $ N $ is divided by $ X $ and the remainder is 0, N / X * T is output, otherwise (N / X + 1) * T is output. Below are examples of solutions in Python3, C ++, and Java. (Python3 uses Solution 1 </ b>, and C ++ uses the code solved by Solution 2 </ b> as an example solution.)

Example of answer for each language

Example solution in Python3

{ABC176A.py}


import math
n,x,t = map(int,input().split())
A = math.ceil(n/x)
print(A*t)
  • The ceiling function is calculated using the function ceil in the library math.
Example solution in C ++

{ABC176A.cpp}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,x,t;
  cin >> n >> x >> t;
  if (n%x == 0){
    cout << n/x * t << endl;
  }else{
    cout << (n/x + 1) * t << endl;
  }
}

Java answer example

{ABC176A.java}


import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int x = scan.nextInt();
    int t = scan.nextInt();
    if (n%x == 0){
      System.out.println(n/x*t);
    }else{
      System.out.println((n/x+1)*t);
    }
  }
}

Recommended Posts