#include <iostream> #include <vector> #include <math.h> using namespace std; int compare(string s1, string s2) { if (s1.size() < s2.size()) return -1; if (s1.size() > s2.size()) return 1; for (int i = 0; i < s1.size(); ++i) { if (s1[i] < s2[i]) return -1; if (s1[i] > s2[i]) return 1; } return 0; } int main() { ios::sync_with_stdio(false); int N; cin >> N; vector<string> nums(N); long long minChanges = 0; for (int i = 0; i < N; ++i) { cin >> nums[i]; } for (int i = 1; i < N; ++i) { if (compare(nums[i], nums[i - 1]) < 0) { string& greater = nums[i - 1]; int oldsize = nums[i].size(); minChanges += greater.size() - nums[i].size(); nums[i] += greater.substr(nums[i].size(), greater.size() - nums[i].size()); int k = nums[i].size() - 1; for (; k >= oldsize; --k) { if (nums[i][k] != '9') { nums[i][k] += 1; break; } } if (k < oldsize) { k += 1; for (; k < nums[i].size(); ++k) nums[i][k] = '0'; nums[i] += '0'; ++minChanges; } else { k += 1; for (; k < nums[i].size(); ++k) nums[i][k] = '0'; } } else if (nums[i] == nums[i - 1]) { nums[i] += '0'; ++minChanges; } } cout << minChanges << endl; 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <iostream> #include <vector> #include <math.h> using namespace std; int compare(string s1, string s2) { if (s1.size() < s2.size()) return -1; if (s1.size() > s2.size()) return 1; for (int i = 0; i < s1.size(); ++i) { if (s1[i] < s2[i]) return -1; if (s1[i] > s2[i]) return 1; } return 0; } int main() { ios::sync_with_stdio(false); int N; cin >> N; vector<string> nums(N); long long minChanges = 0; for (int i = 0; i < N; ++i) { cin >> nums[i]; } for (int i = 1; i < N; ++i) { if (compare(nums[i], nums[i - 1]) < 0) { string& greater = nums[i - 1]; int oldsize = nums[i].size(); minChanges += greater.size() - nums[i].size(); nums[i] += greater.substr(nums[i].size(), greater.size() - nums[i].size()); int k = nums[i].size() - 1; for (; k >= oldsize; --k) { if (nums[i][k] != '9') { nums[i][k] += 1; break; } } if (k < oldsize) { k += 1; for (; k < nums[i].size(); ++k) nums[i][k] = '0'; nums[i] += '0'; ++minChanges; } else { k += 1; for (; k < nums[i].size(); ++k) nums[i][k] = '0'; } } else if (nums[i] == nums[i - 1]) { nums[i] += '0'; ++minChanges; } } cout << minChanges << endl; return 0; } |