1
已解决
题目描述 Description
给定一个十进制正整数n,写下从1到n的所有整数,然后数一下其中出现的数字“1”的个数。
例如当n=2时,写下1,2。这样只出现了1个“1”;当n=15时,写下1,2,3,4,5,6,7,8,9,10,11,12,13,14,15。这样出现了8个“1”。
输入描述 Input Description
正整数n。1 <= n <= 10000。
输出描述 Output Description
一个正整数,即“1”的个数。
样例输入 Sample Input
15
样例输出 Sample Output
8
很急
0
已采纳
尽管我有一次CE.....
不过我还是肝出了这一题!
我没啥时间了,直接给核心吧!
先在外面定义一个函数,用来计数,也就是:
int Count(int n){
int count = 0;
while(n){
count += (n % 10 == 1)?1:0;
n = n / 10;
}
return count;
}
主函数,用个while:
count = 0;
for(i = 1;i <= n;i++){
count += Count(i);
}
cout<<count;
快踩我!
0
0
0
0
0