C++ code to count number of packs of sheets to be bought

C++Server Side ProgrammingProgramming

Suppose we have four numbers k, n, s and p. To make a paper airplane, Rectangular piece of papers are used. From a sheet of standard size we can make s number of airplanes. A group of k people decided to make n airplanes each. They are going to buy several packs of paper, each of them containing p sheets, and then distribute the sheets between the other people. Each person should have enough sheets to make n different airplanes. We have to count the number of packs should we buy?

So, if the input is like k = 5; n = 3; s = 2; p = 3, then the output will be 4, because they have to buy 4 packs of paper: there will be 12 sheets in total, give 2 sheets to each person.


To solve this, we will follow these steps −

ans := k * ((n + s - 1) / s)
return (ans + p - 1) / p


Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int k, int n, int s, int p){
   int ans = k * ((n + s - 1) / s);
   return (ans + p - 1) / p;
int main(){
   int k = 5;
   int n = 3;
   int s = 2;
   int p = 3;
   cout << solve(k, n, s, p) << endl;


5, 3, 2, 3


Updated on 30-Mar-2022 12:45:28