版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34271269/article/details/53308400
思路 找每一位出现的1的个数就好了 从后往前找
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
long long n;
int main()
{
cin>>n;
long long sum=1;
long long cnt=0;
long long buf=n;
long long pos=0;
while(buf)
{
buf/=10;
pos++;
}
for(int i=0;i<pos;i++)
{
cnt+=n/sum/10*sum;
if((n/sum)%10==1)
{
cnt+=n%sum+1;
}
else if((n/sum)%10!=0)
cnt+=sum;
//cout<<cnt<<endl;
sum*=10;
}
cout<<cnt<<endl;
return 0;
}