Skip to content

Instantly share code, notes, and snippets.

@valuks
Created Nov 7, 2021
Embed
What would you like to do?
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
// using namespace std;
namespace patch
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
int str_to_int(const std::string& line) {
std::istringstream is(line);
int v;
is >> v;
return v;
}
void l(std::string log) {
std::cout << log << std::endl;
}
void l(int log) {
std::cout << log << std::endl;
}
void l(std::vector <int> log) {
int log_size = log.size();
for (int i = 0; i < log_size; i++) {
std::cout << log[i] << ' ';
}
std::cout << std::endl;
}
template <typename T>
std::vector<T> slicing(std::vector<T> const& v,
int X, int Y)
{
return std::vector<T>(v.begin() + X, v.begin() + X + Y);
}
template <typename T>
std::vector<T> slicing(std::vector<T> const& v,
int X)
{
return std::vector<T>(v.begin() + X, v.end());
}
std::vector <int> resort(std::vector <int> digits, int i, std::vector <int> digits_original) {
std::vector <int> digits_first_part;
std::vector <int> digits_resort;
std::vector <int> result;
digits_first_part = slicing(digits, i);
digits_resort = slicing(digits, 0, i);
std::sort(digits_resort.begin(), digits_resort.end());
std::reverse(digits_first_part.begin(), digits_first_part.end());
digits_first_part.insert( digits_first_part.end(), digits_resort.begin(), digits_resort.end() );
return digits_first_part;
}
std::string get(std::string number) {
int number_size = number.size();
std::vector <int> digits;
std::vector <int> digits_original;
for (int i = number_size - 1; i >= 0; i--) {
digits.push_back( str_to_int( number.substr(i, 1) ) );
}
digits_original = digits;
bool removed = false;
for (int i = 0; i <= number_size; i++) {
if ( digits[i] == 0 && i < number_size ) {
if ( removed ) {
digits[i]++;
digits = resort(digits, i, digits_original);
break;
}
continue;
}
if (!removed) {
digits[i]--;
removed = true;
continue;
}
if (i == number_size) {
digits.push_back(0);
}
if (digits[i] == 9) {
continue;
}
digits[i]++;
digits = resort(digits, i, digits_original);
break;
}
int digits_size = digits.size();
std::string result;
for (int i = 0; i < digits_size; i++) {
result += patch::to_string(digits[i]);
}
return result;
}
int main() {
std::ifstream input_file("naksk.dat");
std::string line;
std::string result;
getline( input_file, line );
result = get(line);
std::ofstream outfile;
outfile.open("naksk.rez");
outfile << result;
outfile.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment