#include <iostream> #include <string> #include <vector> char digitAt(int index, const std::string &number, int trailingZeroes) { if (index < number.length() + trailingZeroes) { if (index < number.length()) { return number[index]; } else { return '0'; } } else { return '.'; } } int main() { std::string line; std::getline(std::cin, line); int amount = std::stoi(line); long added = 0; int previousDigits = 0; int previousTrailingZeroes = 0; std::string previousNumber = ""; for (int i = 0; i < amount; i++) { int previousAll = previousDigits + previousTrailingZeroes; std::string number; std::getline(std::cin, number); int digits = number.length(); int trailingZeroes = 0; if (digits == previousAll) { bool addZeroes = true; for (int j = 0; j < digits; j++) { int digitOfCurrent = digitAt(j, number, trailingZeroes); int digitOfPrevious = digitAt(j, previousNumber, previousTrailingZeroes); if (digitOfCurrent > digitOfPrevious) { addZeroes = false; break; } else if (digitOfCurrent < digitOfPrevious) { break; } } if (addZeroes) { trailingZeroes += previousTrailingZeroes + 1; added++; } } else if (digits < previousAll) { bool addZeroes = true; bool additionalZero = true; for (int j = 0; j < previousAll; j++) { int digitOfCurrent = digitAt(j, number, trailingZeroes); int digitOfPrevious = digitAt(j, previousNumber, previousTrailingZeroes); if (digitOfCurrent == '.') { for (int x = previousAll - 1; x >= j; x--) { wchar_t digit = digitAt(x, previousNumber, previousTrailingZeroes); if (digit != '9') { addZeroes = false; digit++; std::string intermediate(x-j, '\0'); for (int a = j; a < x; a++) { intermediate[a - j] = digitAt(a, previousNumber, previousTrailingZeroes); } number = number + std::string(intermediate); number += digit; trailingZeroes = previousAll - x - 1; added += previousAll - digits; goto outerBreak; } } /* all nines */ addZeroes = true; additionalZero = true; } else if (digitOfCurrent > digitOfPrevious) { addZeroes = true; additionalZero = false; break; } else if (digitOfCurrent < digitOfPrevious) { addZeroes = true; additionalZero = true; break; } outerContinue:; } outerBreak: if (addZeroes) { trailingZeroes += previousAll - digits + (additionalZero ? 1 : 0); added += trailingZeroes; } } previousNumber = number; previousDigits = number.length(); previousTrailingZeroes = trailingZeroes; } std::cout << added << std::endl; }
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <iostream> #include <string> #include <vector> char digitAt(int index, const std::string &number, int trailingZeroes) { if (index < number.length() + trailingZeroes) { if (index < number.length()) { return number[index]; } else { return '0'; } } else { return '.'; } } int main() { std::string line; std::getline(std::cin, line); int amount = std::stoi(line); long added = 0; int previousDigits = 0; int previousTrailingZeroes = 0; std::string previousNumber = ""; for (int i = 0; i < amount; i++) { int previousAll = previousDigits + previousTrailingZeroes; std::string number; std::getline(std::cin, number); int digits = number.length(); int trailingZeroes = 0; if (digits == previousAll) { bool addZeroes = true; for (int j = 0; j < digits; j++) { int digitOfCurrent = digitAt(j, number, trailingZeroes); int digitOfPrevious = digitAt(j, previousNumber, previousTrailingZeroes); if (digitOfCurrent > digitOfPrevious) { addZeroes = false; break; } else if (digitOfCurrent < digitOfPrevious) { break; } } if (addZeroes) { trailingZeroes += previousTrailingZeroes + 1; added++; } } else if (digits < previousAll) { bool addZeroes = true; bool additionalZero = true; for (int j = 0; j < previousAll; j++) { int digitOfCurrent = digitAt(j, number, trailingZeroes); int digitOfPrevious = digitAt(j, previousNumber, previousTrailingZeroes); if (digitOfCurrent == '.') { for (int x = previousAll - 1; x >= j; x--) { wchar_t digit = digitAt(x, previousNumber, previousTrailingZeroes); if (digit != '9') { addZeroes = false; digit++; std::string intermediate(x-j, '\0'); for (int a = j; a < x; a++) { intermediate[a - j] = digitAt(a, previousNumber, previousTrailingZeroes); } number = number + std::string(intermediate); number += digit; trailingZeroes = previousAll - x - 1; added += previousAll - digits; goto outerBreak; } } /* all nines */ addZeroes = true; additionalZero = true; } else if (digitOfCurrent > digitOfPrevious) { addZeroes = true; additionalZero = false; break; } else if (digitOfCurrent < digitOfPrevious) { addZeroes = true; additionalZero = true; break; } outerContinue:; } outerBreak: if (addZeroes) { trailingZeroes += previousAll - digits + (additionalZero ? 1 : 0); added += trailingZeroes; } } previousNumber = number; previousDigits = number.length(); previousTrailingZeroes = trailingZeroes; } std::cout << added << std::endl; } |