#include <iostream> #include <cstdio> #include <vector> using namespace std; int main() { int N; cin >> N; vector<string>number(N); for(int i=0; i<N; i++){ cin >> number[i]; } long long sum=0; for(int i=0; i<N-1; i++){ if(number[i]>=number[i+1] || number[i].length()>number[i+1].length()){ int length=number[i+1].length(); for(int j=0; j<length; j++){ if(number[i][j]==number[i+1][j] && j!=number[i+1].length()-1){ continue; } if(number[i][j]==number[i+1][j]){ bool is_only_nine=true; for(int k=number[i+1].length(); k<number[i].length(); k++){ if(number[i][k]!='9'){ is_only_nine=false; break; } } if(is_only_nine){ int changes=number[i].length()-number[i+1].length()+1; sum+=changes; for(int k=0; k<changes; k++){ number[i+1]+='0'; } }else{ int changes=number[i].length()-number[i+1].length(); sum+=changes; int length_2=number[i+1].length(); for(int k=0; k<changes; k++){ number[i+1]+='0'; } bool if_not=false; for(int k=number[i].length()-1; k>length_2-1; k--){ if(number[i][k]=='9') continue; if(!if_not){ if_not=true; number[i+1][k]=(char((int)number[i][k]+1)); continue; }else{ number[i+1][k]=number[i][k]; } } } break; } else if(number[i][j]>number[i+1][j]){ int changes=number[i].length()-number[i+1].length()+1; sum+=changes; for(int k=0; k<changes; k++){ number[i+1]+='0'; } break; }else if(number[i][j]<number[i+1][j]){ int changes=number[i].length()-number[i+1].length(); sum+=changes; for(int k=0; k<changes; k++){ number[i+1]+='0'; } break; } } } } cout << sum; 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 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 123 124 125 126 127 | #include <iostream> #include <cstdio> #include <vector> using namespace std; int main() { int N; cin >> N; vector<string>number(N); for(int i=0; i<N; i++){ cin >> number[i]; } long long sum=0; for(int i=0; i<N-1; i++){ if(number[i]>=number[i+1] || number[i].length()>number[i+1].length()){ int length=number[i+1].length(); for(int j=0; j<length; j++){ if(number[i][j]==number[i+1][j] && j!=number[i+1].length()-1){ continue; } if(number[i][j]==number[i+1][j]){ bool is_only_nine=true; for(int k=number[i+1].length(); k<number[i].length(); k++){ if(number[i][k]!='9'){ is_only_nine=false; break; } } if(is_only_nine){ int changes=number[i].length()-number[i+1].length()+1; sum+=changes; for(int k=0; k<changes; k++){ number[i+1]+='0'; } }else{ int changes=number[i].length()-number[i+1].length(); sum+=changes; int length_2=number[i+1].length(); for(int k=0; k<changes; k++){ number[i+1]+='0'; } bool if_not=false; for(int k=number[i].length()-1; k>length_2-1; k--){ if(number[i][k]=='9') continue; if(!if_not){ if_not=true; number[i+1][k]=(char((int)number[i][k]+1)); continue; }else{ number[i+1][k]=number[i][k]; } } } break; } else if(number[i][j]>number[i+1][j]){ int changes=number[i].length()-number[i+1].length()+1; sum+=changes; for(int k=0; k<changes; k++){ number[i+1]+='0'; } break; }else if(number[i][j]<number[i+1][j]){ int changes=number[i].length()-number[i+1].length(); sum+=changes; for(int k=0; k<changes; k++){ number[i+1]+='0'; } break; } } } } cout << sum; return 0; } |