Next highest int from digits: Difference between revisions

Added C++ solution
(Added C++ solution)
Line 56:
;Optional stretch goal:
* 9589776899767587796600
 
=={{header|C++}}==
{{trans|Factor}}
{{libheader|GMP}}
This solution makes use of std::next_permutation, which is essentially the same as Algorithm 2.
<lang cpp>#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>
 
#include <gmpxx.h>
 
using integer = mpz_class;
 
std::string to_string(const integer& n) {
std::ostringstream out;
out << n;
return out.str();
}
 
integer next_highest(const integer& n) {
std::string str(to_string(n));
if (!std::next_permutation(str.begin(), str.end()))
return 0;
return integer(str);
}
 
int main() {
for (integer n : {0, 9, 12, 21, 12453, 738440, 45072010, 95322020})
std::cout << n << " -> " << next_highest(n) << '\n';
integer big("9589776899767587796600");
std::cout << big << " -> " << next_highest(big) << '\n';
return 0;
}</lang>
 
{{out}}
<pre>
0 -> 0
9 -> 0
12 -> 21
21 -> 0
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200
9589776899767587796600 -> 9589776899767587900667
</pre>
 
=={{header|Factor}}==
1,777

edits