-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum_of_ones.c
More file actions
76 lines (54 loc) · 1.32 KB
/
num_of_ones.c
File metadata and controls
76 lines (54 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* 《编程之美》上的一道题
* 给定一个正整数,判断该从1到正整数的数列中,1出现的个数
* 如给定12,则数列1,2,3,4,5,6,7,8,9,10,11,12,1出现的个数为5
* 为提高效率,采用递归的方法 */
#include <stdio.h>
int num_of_ones(int num) {
int i,j,k;
int ret = 0;
int power = 1;
int nums[128];
printf("input %d\n", num);
i = 0;
k = num;
while(1) {
j = k % 10; //
nums[i] = j;
k = k / 10;
if (k == 0)
break;
i ++;
power = power * 10;
}
printf("i %d, power %d, nums[i] %d\n", i, power, nums[i]);
if (i == 0) {
if (num == 0)
return 0;
else
return 1;
}
ret = 0;
if (nums[i] > 1) {
for (j = i; j >= 0; j--) {
if (j == i)
ret += power;
else
ret += power / 10 * nums[i];
}
}
else {
ret += power / 10 * i;
ret += (num - power * nums[i] + 1);
}
printf("ret %d \n", ret);
return (ret + num_of_ones(num - power * nums[i]));
}
int main(int argc, char *argv[])
{
int input = atoi(argv[1]);
int result;
result = num_of_ones(input);
printf("result %d\n", result);
return 0L;
}