Opera & B2G1

siamsplash.5 DIU Take-Off Programming...

Category: Simple Math

Problem Analysis:

Observation: This problem can be solved by simple iteration or by using mathematical observations. As the problem category is Simple Math, let’s solve it with math.

  • Let’s forget about the term “Buy 2 Get 1”. How many pizzas actually you can buy?

  • If for buying 22 pizzas, you’re getting 11 additional pizza for free, then for buying CC pizzas, how many additional pizzas will you get?

Solution Idea:

  • If the pizza price is XX, and the dollar you have is MM. Then the total number of pizzas you can buy is, C=M/XC = M/X

  • For example, M=20M = 20, X=6X = 6, then C= (20/6)=3C =  (20/6) = 3.

  • Now you have the CC. So, the additional pizzas you will get will be,

    =C/2=C/2

    =(M/X)/2= (M/X)/2 [ As, C=M/XC = M/X]

    =M/(2X)= M/(2*X)

  • So, total number of pizzas will be =(M/X)+M/(2X)= (M/X) + {M/(2*X)}

Special Thanks: Md. Albin Hossain

Source Code:
#include <stdio.h>
int main(){
    int test_case;
    scanf("%d", &test_case);
    while(test_case--){
        int m, x;
        scanf("%d %d", &m, &x);
        int pizza_count = (m / x) + (m / (2 * x));
        printf("%d\n", pizza_count);
    }
    return 0;
}

Statistics

92% Solution Ratio
steinumEarliest, 6M ago
user.3193Fastest, 0.0s
Monjurul.9781Lightest, 4.9 MB
ItzRAYShortest, 93B
Toph uses cookies. By continuing you agree to our Cookie Policy.