C++ program to find maximum possible amount of allowance after playing the game

C++Server Side ProgrammingProgramming

Suppose we have three numbers A, B and C. Consider a game: There are three "integer panels", each with a digit form 1 to 9 (both inclusive) printed on it, and one "operator panel" with a '+' sign printed on it. The player should make a formula of the form X+Y, by arranging the four panels from left to right. Then, the amount of the allowance will be equal to the resulting value of the formula.

We have to find the maximum possible amount of the allowance.

So, if the input is like A = 1; B = 5; C = 2, then the output will be 53, because the panels are arranged like 52+1, and this is the maximum possible amount.

Steps

To solve this, we will follow these steps −

Define an array V with A, B and C
sort the array V
ans := (V[2] * 10) + V[1] + V[0]
return ans

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

int solve(int A, int B, int C){
   vector<int> V = { A, B, C };
   sort(V.begin(), V.end());
   int ans = (V[2] * 10) + V[1] + V[0];
   return ans;
}
int main(){
   int A = 1;
   int B = 5;
   int C = 2;
   cout << solve(A, B, C) << endl;
}

Input

1, 5, 2

Output

53
raja
Updated on 03-Mar-2022 07:40:24

Advertisements