Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class Composition {
- public:
- Composition(int number) : number(number) {
- box.emplace_back(to_string(number));
- }
- vector<string> result() {
- Compare comp;
- run();
- sort(box.begin(), box.end(), comp);
- return box;
- }
- private:
- int number;
- char buffer[0xFFFF];
- vector<string> box;
- struct Compare {
- bool operator()(const string& a, const string& b) {
- return a.length() < b.length();
- }
- };
- void decomposition(int num, int beg, int pos) {
- int end = strlen(buffer);
- _itoa_s(num, buffer + pos, sizeof(int), 10);
- int next, limit;
- buffer[end] = 0;
- box.push_back(string(buffer));
- for (next = beg, limit = (num >> 1); next <= limit; ++next) {
- _itoa_s(next, buffer + pos, sizeof(int), 10);
- end = strlen(buffer);
- buffer[end] = ' ';
- decomposition(num - next, next, end + 1);
- }
- }
- void run() {
- int next, limit, length;
- for (next = 1, limit = (number >> 1); next <= limit; ++next) {
- _itoa_s(next, buffer, sizeof(int), 10);
- length = strlen(buffer);
- buffer[length] = ' ';
- decomposition(number - next, next, length + 1);
- }
- }
- };
- int main() {
- int n = 10;
- Composition сomposition(n);
- auto result = сomposition.result();
- auto x = 0;
- for (const auto& item : result) {
- cout << ++x << ") " << item << '\n';
- }
- system("pause > nul");
- }
Add Comment
Please, Sign In to add comment