1049. Counting Ones (30)
时间限制
10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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:12Sample Output:
5
#include<stdio.h> int count[11];//保存0-9分别出现的次数 int value; void deal(int n) { if(n <= 0) return ; int one, ten;//分别表示个位和十位 one = n % 10; n /= 10; ten = n; for(int i = 0; i <= one; i ++) count[i] += value; while(ten) { count[ten%10] += (one+1) * value; ten /= 10; } for(int i = 0; i < 10; i ++) count[i] += value*n; count[0] -= value; value *= 10; deal(n-1); } int main() { int n; scanf("%d", &n); for(int i = 0; i < 10; i ++) count[i] = 0; value = 1; deal(n); printf("%d\n", count[1]); return 0; }