-
Notifications
You must be signed in to change notification settings - Fork 0
/
veci.cpp
65 lines (51 loc) · 1.36 KB
/
veci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @file veci.cpp
* @author William Weston
* @brief Veci Problem From Kattis
* @version 0.1
* @date 2023-07-29
*
* @copyright Copyright (c) 2023
*
* Source: https://open.kattis.com/problems/veci
*
* Your program will be given an integer X.
* Find the smallest number larger than X consisting of the same digits as X.
*/
#include <algorithm>
#include <charconv>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <string>
#include <string_view>
auto string_to_int( std::string_view number ) -> int;
auto main() -> int
{
for ( auto X = std::string(); std::cin >> X; )
{
auto const integer = string_to_int( X );
std::sort( X.begin(), X.end() );
auto min = std::numeric_limits<int>::max();
do
{
auto const permutation = string_to_int( X );
if ( permutation > integer )
{
if ( permutation < min )
min = permutation;
}
}
while ( std::next_permutation( X.begin(), X.end() ) );
std::cout << ( min == std::numeric_limits<int>::max() ? 0 : min ) << '\n';
}
return EXIT_SUCCESS;
}
auto string_to_int( std::string_view number ) -> int
{
auto const first = number.data();
auto const last = first + number.size();
int return_value;
std::from_chars( first, last, return_value );
return return_value;
}