#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int len; bool first = true; bool second = true; int printed = 0; void print(int combination[7]) { int intresult = 0; if (first) { printed = 0; first = false; second = true; return; } if (second) { second = false; } else { cout << " "; } printed++; //for (int i = len - 1; i >= 0; i--) for (int i = 0; i < len; i++) { // cout << combination[i]; intresult *= 10; intresult += combination[i]; } cout << setw(len) << setfill('0') << intresult; } int* select(int combination[7], bool fixed[7]) { int maxscore = -1, maxi = -1; /* for (int i = 0; i < len; i++) { if (!fixed[i]) { int score; if (combination[i] > 4) { score = combination[i] - 5; } else { score = combination[i]; } if (score > maxscore) { maxi = i; } } }*/ for (int i = 0; i < len; i++) { if (!fixed[i]) { maxi = i; // fixed[i]=true; break; } } //if (maxscore > -1) if (maxi > -1) { fixed[maxi] = true; int order[10]; int cur = combination[maxi]; if (cur > 4) { int o = 0; for (int i = cur; i <= 9; i++) { order[o] = i; o++; } for (int i = cur - 1; i >= 0; i--) { order[o] = i; o++; } } else { int o = 0; for (int i = cur; i >= 0; i--) { order[o] = i; o++; } for (int i = cur + 1; i <= 9; i++) { order[o] = i; o++; } } // int *combcopy = new int[7]; // int* combresult; for (int i = 0; i < 10; i++) { //int combcopy[7]; bool fixedcopy[7]; for (int j = 0; j < len; j++) { // combcopy[j] = combination[j]; fixedcopy[j] = fixed[j]; } // combcopy[maxi] = order[i]; combination[maxi] = order[i]; //combresult = select(combcopy, fixedcopy); combination = select(combination, fixedcopy); // combination = combresult; if (order[i] == 0 && i != 9) { for (int j = 1; j < order[i + 1]; j++) { /*combresult[maxi] = j; print(combresult);*/ combination[maxi] = j; print(combination); } } else if (order[i] == 9 && i != 9) { for (int j = 8; j > order[i + 1]; j--) { /*combresult[maxi] = j; print(combresult);*/ combination[maxi] = j; print(combination); } } } // delete return combination; } else { print(combination); /* int intresult = 0; for (int i = len - 1; i >= 0; i++) { intresult *= 10; intresult += combination[i]; }*/ //result[count] = intresult; //count++; return combination; } } //int result[10000000]; //int count = 0; int main(){ string comb; while (true) { first = true; cin >> comb; if (comb == "-1") { break; } len = (int)comb.size(); int combination[7]; bool fixed[7]; for (int i = 0; i < len; i++) { combination[i] = comb[i] - '0'; fixed[i] = false; } int t = 0; for (int i = 0; i < len; i++) { if (combination[i] >= 5) { t += 9 - combination[i]; } else { t += combination[i]; } } int result = 1; for (int i = 0; i < len; i++) { result *= 10; } cout << t + result - 1 << endl; select(combination, fixed); cout << endl; } return 0; }