It's a permutation problem, the following code is quite terse, but I believe you can figure it out. Good luck!

```
class Solution
{
public:
int countDigitOne(int num)
{
int sum = 0;
for(long i = 1; i <= num; i *= 10)
{
int a = num/i;
int b = num%i;
sum += (a+8)/10*i+(a%10==1)*(b+1);
}
return sum;
}
};
```