Write a function solution that given a three digit integer N and Integer K, returns the maximum possible three digit value that can be obtained by performing at most K increases by 1 of any digit in N
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include<string.h>
using namespace std;
void GetMax(string InputS, int K, string& maxval)
{
if (K == 0) return;
int l = InputS.length();
for (int i = 0; i < l - 1; i++)
{
for (int m = i + 1; m < l; m++)
{
if (InputS[i] < InputS[m])
{
swap(InputS[i], InputS[m]);
if (InputS.compare(maxval) > 0) maxval = InputS;
GetMax(InputS, K - 1, maxval);
swap(InputS[i], InputS[m]);
}
}
}
}
int main()
{
string Sn,max ;
int K = 0;
cout<<"Enter a 3-digits number (N): "; cin>>Sn;
cout<<"Enter K : "; cin>>K;
max = Sn;
GetMax(Sn, K, max);
cout <<"\n\tOutput: "<<max<<"\n";
return 0;
}
Comments
Leave a comment