1049. Counting Ones 解析
来源:互联网 发布:linux怎么安装ftp服务 编辑:程序博客网 时间:2024/06/08 13:59
题目是要求数1-N的数字里面一共有多少个数字1,别理解成含有1的数字的个数了。
如果有《编程之美》这本书可以看看里面的介绍。讲的不错的。
我这里就贴两个图大概说下规律。佩服总结出这个规律的人。。。
#include <iostream>#include <vector>#include <string>#include <algorithm>#include <map>#include <queue>#include <stack>#include <cstring>using namespace std;long long n,ans=0;int main(){scanf("%lld",&n);long long pre,now,low;long long fac = 1;while(n/fac!=0){low = n % fac;pre = n / (10* fac);now = n / fac %10;if(now == 0)ans+=pre*fac;else if(now == 1)ans+=pre*fac+low+1;elseans += ((pre+1)*fac);fac*=10;}cout << ans << endl;return 0 ;}
阅读全文
0 0
- 1049. Counting Ones 解析
- 1049. Counting Ones
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049. Counting Ones
- 1049. Counting Ones (30)
- PAT 1049. Counting Ones
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049.Counting Ones
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 1049. Counting Ones (30)
- 原型模式
- 【Java web】get、put、post、delete请求方法总结
- CSDN日报20170712——《AI 大行其道,你准备好了吗?》 标签: csdn程序人生 2017-07
- SQL Developer12 中文+破解版
- More is better (并查集)
- 1049. Counting Ones 解析
- JDBC之ParameterMetaData
- PAT1016~~~
- 欢迎使用CSDN-markdown编辑器
- 【c#】变量的作用域
- 99%的人将沦为无用阶层,你会是那1%吗
- 点击ImageView旋转固定角度
- 【c#】函数重载的条件
- Java内存模型