#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef unsigned int uint; typedef unsigned char uchar; typedef unsigned long long ulonglong; bool hasInput(); void readInput(); void calculate(); void writeOutput(); void reset(); uint numRows = 0; int* dir; bool nextp(int* num, int l, int i); bool next(int* num, int l, int i); int main(int argc, char **argv) { string str; cin >> str; while (str != "-1"){ int l = str.length(); int* num = new int[l]; int a = 0; int b = 1; dir = new int[l]; for (int i=0; i < l; i++) { num[i] = str[i] - '0'; if (num[i]<5) { a+= num[i]; dir[i] = -1; // down } else { a+= (9 - num[i]); dir[i] = 1; // up; } b *= 10; } cout << b+a-1 << endl; int p = b+a-1; for (int i=0; i 0) { p--; next(num, l, 0); for (int i=0; i > str; } return 0; } bool nextp(int* num, int l, int i) { if (dir[i] > 0) {// up if (num[i]<9) { num[i]++; }else { dir[i] = -2; //down return false; } } else {// down if (num[i]>0) { num[i]--; }else { dir[i] = 2;//up return false; } } return true; } bool next(int* num, int l, int i) { if (i > l) return true; // finish if (dir[i] > 0) {// up if (num[i]<9) { num[i]++; }else { dir[i] = -2; //down return next(num, l, i+1); } } else {// down if (num[i]>0) num[i]--; else { dir[i] = 2;//up return next(num, l, i+1); } } return false; }