代码如下:
#include <iostream>
#include <math.h>
using namespace std;
#define N 11
void GetAllBit(int x, int& len, int bit[])
{
len = 0;
bit[len] = x % 10;
while( x )
{
x = x / 10;
bit[++len] = x % 10;
}
}
int GetOneNum(int x, int len, int bit[])
{
if( x == 0)
return 0;
int h = bit[len-1];
int tmp1 = pow(10,len-1);
int tmp2 = h*(len-1)*tmp1/10;
if( h == 0)
{
tmp2 = 0;
}
else if( h == 1)
{
tmp2 += x-tmp1+1;
}
else
{
tmp2 += tmp1;
}
return tmp2 + GetOneNum(x-h*tmp1, len-1, bit);
}
void main()
{
int x = 100,len,bit[N];
int num;
GetAllBit(x,len,bit);
num = GetOneNum(x,len,bit);
cout<< num <<endl;
}