A41074. 数1的个数
填空题
较易
知识点
题目描述
数1的个数
题目描述
给定一个十进制正整数n,写下从1到n的所有整数,然后数一下其中出现的数字“1”的个数。
例如当n=2时,写下1,2。这样只出现了1个“1”;当n=12时,写下1,2,3,4,5,6,7,8,9,10,11,12。这样出现了5个“1”。
输入格式
正整数n。1 <= n <= 10000。
输出格式
一个正整数,即“1”的个数。
样例输入
12
样例输出
5
参考答案
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
long long int Count(long long int n){
long long int count = 0;
while(n){
count += (n % 10 == 1)?1:0;
n = n / 10;
}
return count;
}
int main()
{
long long int n,i,count;
while(scanf("%lld",&n) != EOF){
count = 0;
for(i = 1;i <= n;i++){
count += Count(i);
}
printf("%lld\n",count);
}
return 0;
}
上一题
下一题