Minimize the cost of buying the Objects | C ++ program of above approach ; Function that will calculate the price ; Calculate the number of items we can get for free ; Calculate the number of items we will have to pay the price for ; Calculate the price ; Driver code ; Calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int totalPay ( int totalItems , int priceOfOneItem , int N , int M ) { int freeItems = 0 , actual = 0 ; freeItems = totalItems / ( N + M ) ; actual = totalItems - freeItems ; int amount = actual * priceOfOneItem ; return amount ; } int main ( ) { int T = 12 , P = 8 ; int N = 2 , M = 1 ; cout << " Amount ▁ = ▁ " << totalPay ( T , P , N , M ) ; return 0 ; }