#include <stdio.h> #include <string.h> const int MAX_L = 11; int main() { int i, j, k, prev_len, n, added = 0; char buf1[MAX_L], buf2[MAX_L]; char *akt = buf1, *prev = buf2, *tmp; scanf("%d", &n); scanf("%10s", prev); prev_len = strlen(prev); for (i = 1; i < n; ++i) { scanf("%10s", akt); // printf("%s %s\n", prev, akt); for (j = 0; j < prev_len && akt[j] == prev[j]; ++j); if (akt[j] == '\0' && prev[j] == '\0') { akt[j++] = '0'; added += 1; } else if (akt[j] != '\0') { if (akt[j] > prev[j]) { for (; akt[j] != '\0'; ++j); for (; j < prev_len; ++j) { akt[j] = '0'; added += 1; } } else if (akt[j] < prev[j]) { for (; akt[j] != '\0'; ++j); for (; j < prev_len + 1; ++j) { akt[j] = '0'; added += 1; } } } else { for (k = prev_len - 1; k >= j && prev[k] == '9'; --k); if (k < j) { for (; j < prev_len + 1; ++j) { akt[j] = '0'; added += 1; } } else { for (; j < prev_len; ++j) { if (j < k) { akt[j] = prev[j]; } else if (j == k) { akt[j] = prev[j] + 1; } else { akt[j] = '0'; } added += 1; } } } akt[j] = '\0'; prev_len = j; tmp = akt; akt = prev; prev = tmp; } // printf("%s %s\n", prev, akt); printf("%d\n", added); 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 57 58 59 60 61 62 63 64 65 | #include <stdio.h> #include <string.h> const int MAX_L = 11; int main() { int i, j, k, prev_len, n, added = 0; char buf1[MAX_L], buf2[MAX_L]; char *akt = buf1, *prev = buf2, *tmp; scanf("%d", &n); scanf("%10s", prev); prev_len = strlen(prev); for (i = 1; i < n; ++i) { scanf("%10s", akt); // printf("%s %s\n", prev, akt); for (j = 0; j < prev_len && akt[j] == prev[j]; ++j); if (akt[j] == '\0' && prev[j] == '\0') { akt[j++] = '0'; added += 1; } else if (akt[j] != '\0') { if (akt[j] > prev[j]) { for (; akt[j] != '\0'; ++j); for (; j < prev_len; ++j) { akt[j] = '0'; added += 1; } } else if (akt[j] < prev[j]) { for (; akt[j] != '\0'; ++j); for (; j < prev_len + 1; ++j) { akt[j] = '0'; added += 1; } } } else { for (k = prev_len - 1; k >= j && prev[k] == '9'; --k); if (k < j) { for (; j < prev_len + 1; ++j) { akt[j] = '0'; added += 1; } } else { for (; j < prev_len; ++j) { if (j < k) { akt[j] = prev[j]; } else if (j == k) { akt[j] = prev[j] + 1; } else { akt[j] = '0'; } added += 1; } } } akt[j] = '\0'; prev_len = j; tmp = akt; akt = prev; prev = tmp; } // printf("%s %s\n", prev, akt); printf("%d\n", added); return 0; } |