基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数。

 
例如:n = 12,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1。
Input
输入N(1 <= N <= 10^9)
Output
输出包含1的个数
Input示例
12
Output示例
5
详解请看大牛博客:http://www.cnblogs.com/jy02414216/archive/2011/03/09/1977724.html
之前一直不明白为什么要循环,原来自己把题解看得太死了,百位是举例子,意思是判断每一个当前的数字都得这么算。。。
 #include<bits/stdc++.h>
#define LL long long
using namespace std;
LL f(LL n) {
LL count = ;
LL i = ;
LL current = , after = , before = ;
//int t=0;
while ((n / i) != ) {
current = (n / i) % ;
before = n / (i * );
after = n - (n / i) * i;
if (current > )
count = count + (before + ) * i;
else if (current == )
count = count + before * i;
else if (current == )
count = count + before * i + after + ;
i = i * ;
//printf("t:%d before:%d current:%d after:%d count:%d\n",++t,before,current,after,count);
}
return count;
}
int main(){
LL n;
cin>>n;
cout<<f(n)<<endl;
}

最新文章

  1. Jenkins创建Maven项目及SSH部署
  2. html5——canva 绘图1简单图形
  3. Linux 安装tomcat
  4. 兼容90%标准C的词法分析器
  5. 【bzoj1178】 Apio2009—CONVENTION会议中心
  6. 使用CSS3的appearance属性改变元素的外观
  7. CentOS 6 安装 python 2.7 和 mod_wsgi 运行pyocr[tesseract]
  8. adb shell 命令详解(转)
  9. 9、Http回顾/Servlet
  10. jeecms v7
  11. Android解决异常apk on device &#39;0292bea1&#39;: Unable to open sync connection!
  12. ArcGIS课程:表面数据转换成矢量数据
  13. 优化算法-BFGS
  14. 蓝桥杯-无穷分数-java
  15. Canvas 实现灵动的红鲤鱼动画(上)
  16. 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…【字符串+模拟】
  17. 机器学习技法:02 Dual Support Vector Machine
  18. 简单RPC实现之Netty实现
  19. [How To] TrueCrypt使用教學 - 重要資訊的加密保險箱(转)
  20. ionic中执行pop返回上一个页面,还需要执行操作

热门文章

  1. (转)Yale CAS + .net Client 实现 SSO(5)
  2. maxlength属性在textarea里奇怪的表现
  3. 应用型GIS 地理信息系统设计内容和方法
  4. SNRO:Number Range
  5. IOS基础之设置APP的名字、设置图标、添加等待加载时的图片
  6. 使用winform来递归实现资源管理器
  7. Winform 拦截最小化、最大化、关闭事件【整理】
  8. 使用socket.io搭建聊天室
  9. Linux下ThinkPHP网站目录权限设置
  10. require.js入门指南(二)