#include "iostream" #include "string" using namespace std; int n, a, b, c; // c -wynik string sa, sb; int main() { cin.tie(); iostream::sync_with_stdio(0); cin >> n; cin >> sa; a = sa.size(); for (int i = 1; i < n; i++) { cin >> sb; b = sb.size(); if (b <= a) { bool changed = false; string st; int bp = b; while (b < a) { b++; sb += '0'; c++; } for (int j = 0; j < a; j++) { if (sa[j] < sb[j]) { changed = true; break; } else if (sa[j] > sb[j]) break; } if (b <= a && !changed) { for (int j = a-1; j >= 0; j--) { if (sa[j] < sb[j]) { changed = false; break; } if(sa[j]!='9'&&bp<=j) { sb.replace(j, j, to_string(atoi(&sa[j]) + 1)); changed = true; break; } } if (!changed) { b++; c++; sb += '0'; } } //cout << "sb:" << sb << endl; } a = b; sa = sb; } cout << c; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include "iostream" #include "string" using namespace std; int n, a, b, c; // c -wynik string sa, sb; int main() { cin.tie(); iostream::sync_with_stdio(0); cin >> n; cin >> sa; a = sa.size(); for (int i = 1; i < n; i++) { cin >> sb; b = sb.size(); if (b <= a) { bool changed = false; string st; int bp = b; while (b < a) { b++; sb += '0'; c++; } for (int j = 0; j < a; j++) { if (sa[j] < sb[j]) { changed = true; break; } else if (sa[j] > sb[j]) break; } if (b <= a && !changed) { for (int j = a-1; j >= 0; j--) { if (sa[j] < sb[j]) { changed = false; break; } if(sa[j]!='9'&&bp<=j) { sb.replace(j, j, to_string(atoi(&sa[j]) + 1)); changed = true; break; } } if (!changed) { b++; c++; sb += '0'; } } //cout << "sb:" << sb << endl; } a = b; sa = sb; } cout << c; return 0; } |