#include <cstdio> #include <vector> #include <string> #include <iostream> int main() { int gamesNumber; long long int numberOfChanges = 0; scanf("%d\n", &gamesNumber); int counter = 1; std::string lastGamePrice; std::getline (std::cin, lastGamePrice); while (counter < gamesNumber) { counter++; std::string currentGamePrice; std::getline (std::cin, currentGamePrice); int missingDigits = 0; int lastLength = lastGamePrice.length(); int currentLength = currentGamePrice.length(); if (currentLength <= lastLength) { missingDigits += (lastLength - currentLength); int comparedIndex = 0; while (comparedIndex < currentLength && lastGamePrice[comparedIndex] == currentGamePrice[comparedIndex]) { comparedIndex++; } if (comparedIndex < currentLength && (lastGamePrice[comparedIndex] > currentGamePrice[comparedIndex])) { missingDigits += 1; currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } else if (comparedIndex == currentLength) { bool isAllNineEnd = true; if (comparedIndex == lastLength) { missingDigits += 1; currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } else { std::vector<char> digitsToAdd; while (comparedIndex < lastLength) { char currentDigit = lastGamePrice[comparedIndex]; comparedIndex++; isAllNineEnd = isAllNineEnd && currentDigit == '9'; if (currentDigit != '9') { currentDigit++; } digitsToAdd.push_back(currentDigit); } if (isAllNineEnd) { missingDigits += 1; currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } else { currentGamePrice.insert(currentGamePrice.end(), digitsToAdd.begin(), digitsToAdd.end()); } } } else { currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } } numberOfChanges += missingDigits; lastGamePrice = currentGamePrice; } printf("%llu\n", numberOfChanges); 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 | #include <cstdio> #include <vector> #include <string> #include <iostream> int main() { int gamesNumber; long long int numberOfChanges = 0; scanf("%d\n", &gamesNumber); int counter = 1; std::string lastGamePrice; std::getline (std::cin, lastGamePrice); while (counter < gamesNumber) { counter++; std::string currentGamePrice; std::getline (std::cin, currentGamePrice); int missingDigits = 0; int lastLength = lastGamePrice.length(); int currentLength = currentGamePrice.length(); if (currentLength <= lastLength) { missingDigits += (lastLength - currentLength); int comparedIndex = 0; while (comparedIndex < currentLength && lastGamePrice[comparedIndex] == currentGamePrice[comparedIndex]) { comparedIndex++; } if (comparedIndex < currentLength && (lastGamePrice[comparedIndex] > currentGamePrice[comparedIndex])) { missingDigits += 1; currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } else if (comparedIndex == currentLength) { bool isAllNineEnd = true; if (comparedIndex == lastLength) { missingDigits += 1; currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } else { std::vector<char> digitsToAdd; while (comparedIndex < lastLength) { char currentDigit = lastGamePrice[comparedIndex]; comparedIndex++; isAllNineEnd = isAllNineEnd && currentDigit == '9'; if (currentDigit != '9') { currentDigit++; } digitsToAdd.push_back(currentDigit); } if (isAllNineEnd) { missingDigits += 1; currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } else { currentGamePrice.insert(currentGamePrice.end(), digitsToAdd.begin(), digitsToAdd.end()); } } } else { currentGamePrice.insert(currentGamePrice.end(), missingDigits, '0'); } } numberOfChanges += missingDigits; lastGamePrice = currentGamePrice; } printf("%llu\n", numberOfChanges); return 0; } |