#include <bits/stdc++.h>
using namespace std;
bool cmp(const string & a, const string & b)
{
if(a.size() > b.size())
return true;
if(a.size() < b.size())
return false;
for(int i = 0; i < (int)a.size(); i++)
{
if(a[i] > b[i])
return true;
if(b[i] > a[i])
return false;
}
return true;
}
int main()
{
int N;
cin>>N;
string a,b;
long long result = 0;
cin>>a>>b;
for(int i = 0; i < N - 1; i++)
{
if(cmp(a,b))
{
long long c = a.size() - b.size();
if(a[0] < b[0])
{
for(long long j = 0; j < c; j++)
b += "0";
result += c;
}
else if(a[0] == b[0])
{
for(long long j = 0; j < c; j++)
{
char d = a[b.size()];
if(d != '9')
{
b += a[b.size()]+1;
}
else
{
b += "9";
}
}
result += c;
if(cmp(a,b))
{
b += "0";
result++;
}
}
else if(a[0] > b[0])
{
for(long long j = 0; j <= c; j++)
b += "0";
result += c + 1;
}
}
a = b;
cin>>b;
}
cout<<result<<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 | #include <bits/stdc++.h> using namespace std; bool cmp(const string & a, const string & b) { if(a.size() > b.size()) return true; if(a.size() < b.size()) return false; for(int i = 0; i < (int)a.size(); i++) { if(a[i] > b[i]) return true; if(b[i] > a[i]) return false; } return true; } int main() { int N; cin>>N; string a,b; long long result = 0; cin>>a>>b; for(int i = 0; i < N - 1; i++) { if(cmp(a,b)) { long long c = a.size() - b.size(); if(a[0] < b[0]) { for(long long j = 0; j < c; j++) b += "0"; result += c; } else if(a[0] == b[0]) { for(long long j = 0; j < c; j++) { char d = a[b.size()]; if(d != '9') { b += a[b.size()]+1; } else { b += "9"; } } result += c; if(cmp(a,b)) { b += "0"; result++; } } else if(a[0] > b[0]) { for(long long j = 0; j <= c; j++) b += "0"; result += c + 1; } } a = b; cin>>b; } cout<<result<<endl; } |
English