I was solving http://codeforces.com/problemset/problem/552/B.
In my first attempt I came up with something like:
#include <bits/stdc++.h>
using namespace std;
int digit(long a){
int i=0;
while(a){
a/=10;
i++;
}
return i;
}
int main()
{
long n;
long long s=0;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
int dig=digit(n),i=0;
while(i<dig){
s+=(n-pow(10,i)+1);
i++;
}
cout<<s;
return 0;
}
But for input
1000000
My program outputed
5888895
I was expecting
5888896
In my second try I wrote pow function for myself:
#include <bits/stdc++.h>
using namespace std;
int digit(long a){
int i=0;
while(a){
a/=10;
i++;
}
return i;
}
long long pow1(int a){
long long s=1;
while(a--){
s*=10;
}
return s;
}
int main()
{
long n;
long long s=0;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
int dig=digit(n),i=0;
while(i<dig){
long long aux=pow1(i);
s+=(n-aux+1);
i++;
}
cout<<s;
return 0;
}
And this time it was correct.How can one explain the working behind it?