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