Given a three-digit integer N and an 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<iostream>
#include<string>
#include <sstream>
using namespace std;
int maximum(int N, int k){
string str = to_string(N);
int a = str[0]- 48;
for(int i=1; i<=k; i++){
a = a + 1;
}
int b = str[1] - 48;
int c = str[2] - 48;
string res = to_string(a) +""+to_string(b)+""+to_string(c);
stringstream answer(res);
int x = 0;
answer >> x;
return x;
}
int main(){
int n = 445;
cout<<"The maximum possible value is: "<<maximum(n,4)<<endl;
}
Comments
Leave a comment