#include <bits/stdc++.h> using namespace std; const int ac = 20; int A[ac]; int B[ac]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int d = 1; long long s = 0; for (int k = 0; k < n; k++) { int a; cin >> a; int j = 0; while (a) B[j] = a % 10, a /= 10, j++; reverse(B, B + j); if (j > d) { for (int i = 0; i < j; i++) A[i] = B[i]; d = j; continue; } int x = 0; for (int i = 0; i < j; i++) { if (B[i] < A[i]) { x = -1; break; } else if (B[i] > A[i]) { x = 1; break; } } if (x == -1) { for (int i = 0; i < j; i++) A[i] = B[i]; for (int i = j; i < min(17, d + 1); i++) A[i] = 0; d++; } else if (x == 1) { for (int i = 0; i < j; i++) A[i] = B[i]; for (int i = j; i < min(17, d); i++) A[i] = 0; } else { if (d <= 17) { bool b = 0; for (int i = j; i < d; i++) b |= (A[i] != 9); if (!b) { for (int i = 0; i < j; i++) A[i] = B[i]; for (int i = j; i < min(17, d + 1); i++) A[i] = 0; d++; } else { long long l = 0; for (int i = j; i < d; i++) l *= 10, l += A[i]; l++; for (int i = d - 1; i >= j; i--) A[i] = l % 10, l /= 10; for (int i = 0; i < j; i++) A[i] = B[i]; } } } s += d - j; /* for (int i = 0; i < d; i++) cout << A[i]; cout << '\n';*/ } cout << s << '\n'; }
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <bits/stdc++.h> using namespace std; const int ac = 20; int A[ac]; int B[ac]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int d = 1; long long s = 0; for (int k = 0; k < n; k++) { int a; cin >> a; int j = 0; while (a) B[j] = a % 10, a /= 10, j++; reverse(B, B + j); if (j > d) { for (int i = 0; i < j; i++) A[i] = B[i]; d = j; continue; } int x = 0; for (int i = 0; i < j; i++) { if (B[i] < A[i]) { x = -1; break; } else if (B[i] > A[i]) { x = 1; break; } } if (x == -1) { for (int i = 0; i < j; i++) A[i] = B[i]; for (int i = j; i < min(17, d + 1); i++) A[i] = 0; d++; } else if (x == 1) { for (int i = 0; i < j; i++) A[i] = B[i]; for (int i = j; i < min(17, d); i++) A[i] = 0; } else { if (d <= 17) { bool b = 0; for (int i = j; i < d; i++) b |= (A[i] != 9); if (!b) { for (int i = 0; i < j; i++) A[i] = B[i]; for (int i = j; i < min(17, d + 1); i++) A[i] = 0; d++; } else { long long l = 0; for (int i = j; i < d; i++) l *= 10, l += A[i]; l++; for (int i = d - 1; i >= j; i--) A[i] = l % 10, l /= 10; for (int i = 0; i < j; i++) A[i] = B[i]; } } } s += d - j; /* for (int i = 0; i < d; i++) cout << A[i]; cout << '\n';*/ } cout << s << '\n'; } |