The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (≤230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:
12
Sample Output:
5
#include<stdio.h>
int CountOnes(int n)
{
int count=0;
int factor=1;
int higher,lower,cur;
while(n/factor!=0)
{
higher=n/(factor*10);
lower=n-(n/factor)*factor;
cur=(n/factor)%10;
switch(cur)
{
case 0:
count+=higher*factor;
break;
case 1:
count+=higher*factor+lower+1;
break;
default:
count+=higher*factor+factor;
}
factor=factor * 10;
}
return count;
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",CountOnes(n));
return 0;
}