提交时间:2023-05-23 21:46:51
运行 ID: 74423
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int sizee; bool flag = false, flg = false, flg8 = false; char s[1001000]; bool check(long long x, long long k) { long long res = 0; while (x) { res += x % 10; x /= 10; } if (res == k) return true; return false; } int main() { scanf("%s", s + 1); sizee = strlen(s + 1); if (s[1] == '-') flg = 1; if (!flg) { int tot = 0; for (int i = 1; i <= sizee; i++) if (s[i] == '8') tot++; if (tot >= sizee - 1) flg8 = 1; } if (sizee == 1 && !flg) { if (s[1] == '9') printf("-19\n"); else printf("%c", s[1] + 1); } else if (sizee <= 7) { long long num = 0, k = 0; if (!flg) { for (int i = 1; i <= sizee; i++) { num = num * 10 + s[i] - '0'; k += s[i] - '0'; } } else { for (int i = 2; i <= sizee; i++) { num = num * 10 + s[i] - '0'; k += s[i] - '0'; } num = -num; } if (num == -999999) printf("-1999999"); else { while (1) { num--; if (check(abs(num), k + 1)) { printf("%lld\n", num); break; } } } } else if (flg8 && !flg) { s[sizee]++; s[sizee - 1]++; s[sizee - 2]--; for (int i = 1; i <= sizee; i++) printf("%c", s[i]); printf("\n"); } else { if (flg) { for (int i = sizee; i >= 2; i--) { if (s[i] != '9') { flag = 1; s[i]++; break; } } if (flag) { for (int i = 1; i <= sizee; i++) printf("%c", s[i]); printf("\n"); } else { printf("-1"); for (int i = 2; i <= sizee; i++) printf("%c", s[i]); printf("\n"); } } else { // int sum = 0; for (int i = sizee; i >= 1; i--) { if (s[i] >= '1' && sum >= 2) { s[i]--; sum = 2; for (int j = i + 1; j <= sizee; j++) { while (sum && s[j] < '9') { sum--; s[j]++; } } int cnt = 0; for (int j = i + 1; j <= sizee; j++) cnt += s[j] - '0'; for (int j = i + 1; j <= sizee; j++) { if (cnt >= 9) { s[j] = '9'; cnt -= 9; } else { s[j] = cnt + '0'; cnt = 0; } } flag = 1; break; } sum += '9' - s[i]; } if (flag) { for (int i = 1; i <= sizee; i++) printf("%c", s[i]); printf("\n"); } // else { if (s[1] != '9') { s[1]++; printf("-"); for (int i = 1; i <= sizee; i++) printf("%c", s[i]); printf("\n"); } else { printf("-1"); for (int i = 1; i <= sizee; i++) printf("%c", s[i]); printf("\n"); } } } } return 0; }