#include <bits/stdc++.h> using namespace std; bool operator<(const string& a, const string& b) { for(int i = 0; i < min(a.size(), b.size()); i++) { if(a[i] < b[i]) return true; else if(a[i] > b[i]) return false; } return false; } bool operator>(const string& a, const string& b) { for(int i = 0; i < min(a.size(), b.size()); i++) { if(a[i] > b[i]) return true; else if(a[i] < b[i]) return false; } return false; } string increment(const string& s) { string t = s; for(int i = s.size() - 1; i >= 0; i++) { if(t[i] == '9') t[i] = '0'; else { ++t[i]; return t; } if(!i) t = "1" + t; } return t; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int x; cin >> x; string prev, next; cin >> prev; long long sum = 0; for(int i = 1; i < x; i++) { cin >> next; int diff = prev.size() - next.size(); sum += max(diff, 0); if(diff > 0 || (!diff && prev >= next)) { if(next > prev) { while(prev.size() > next.size()) next += "0"; } else if(next < prev) { ++sum; while(prev.size() >= next.size()) next += "0"; } else { string temp = increment(prev); if(temp > next) { ++sum; while(prev.size() >= next.size()) next += "0"; } else { next = temp; } } } prev = next; } cout << sum << 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 | #include <bits/stdc++.h> using namespace std; bool operator<(const string& a, const string& b) { for(int i = 0; i < min(a.size(), b.size()); i++) { if(a[i] < b[i]) return true; else if(a[i] > b[i]) return false; } return false; } bool operator>(const string& a, const string& b) { for(int i = 0; i < min(a.size(), b.size()); i++) { if(a[i] > b[i]) return true; else if(a[i] < b[i]) return false; } return false; } string increment(const string& s) { string t = s; for(int i = s.size() - 1; i >= 0; i++) { if(t[i] == '9') t[i] = '0'; else { ++t[i]; return t; } if(!i) t = "1" + t; } return t; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int x; cin >> x; string prev, next; cin >> prev; long long sum = 0; for(int i = 1; i < x; i++) { cin >> next; int diff = prev.size() - next.size(); sum += max(diff, 0); if(diff > 0 || (!diff && prev >= next)) { if(next > prev) { while(prev.size() > next.size()) next += "0"; } else if(next < prev) { ++sum; while(prev.size() >= next.size()) next += "0"; } else { string temp = increment(prev); if(temp > next) { ++sum; while(prev.size() >= next.size()) next += "0"; } else { next = temp; } } } prev = next; } cout << sum << endl; } |