统计"1"个数问题
2015-05-17 16:39
99 查看
问题:
给定一个十进制整数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”。
回答:
#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;
}
给定一个十进制整数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”。
回答:
#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;
}
相关文章推荐
- hadoop "File /user/<user>/input/conf/slaves could only be replicated to 0 nodes, instead of 1"问题及解决办
- 解决"cvCreateVideoWriter保存视频帧压缩格式只能用-1"的问题
- IE兼容问题解决方案——指定文件兼容性为content="IE=Edge,chrome=1"(可以解决很多的问题)
- Android Studio 解决aapt "finished with non-zero exit value 1"问题,aaptnon-zero
- hadoop "File /user/<user>/input/conf/slaves could only be replicated to 0 nodes, instead of 1"问题及解决办
- 黑马程序员-解决系统报错“线程间操作无效: 从不是创建控件" txtbox1" 的线程访问它”问题的方法
- "JVM terminated. Exit code=-1"问题的解决方法
- 对"::1"的问题
- Java编码转换问题,new String(str.getByts("ISO8859-1"), "GBK")的作用
- 《奔跑吧,兄弟》之王祖蓝的"钥匙配箱子"概率统计问题--->>回眸
- 正则表达式问题:如何理解/href\s*=\s*(?:"(?<1>[^"]*)"|(?<1>\S+))/(转载)
- 【问题与思考】1+"1"=?
- page = new String(request.getQueryString().getBytes("ISO-8859-1"),"GBK");解决前台传后台乱码问题
- 解决"cvCreateVideoWriter保存视频帧压缩格式只能用-1"的问题
- 【python】关于_tkinter.TclError: image "pyimage1" doesn't exist 问题的解决办法
- jsp中使用equals()判断字符串是否相等时出现的问题(.equals("1")出现的异常)
- 解决aapt "finished with non-zero exit value 1"问题
- MyEclipse问题:some characters can not mapped using "iso-8859-1" character encoding 解决方法
- 关于Html5中<input type="number" name="points" min="1" max="10" />只能输入整数的问题
- jsp页面中的汉字问题和表单提交汉字问题(***.getBytes("ISO-8859-1")行报错)