PAT甲级1049 Counting Ones【规律】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805430595731456
题意:
给定n,问0~n中,1的总个数是多少。
思路:
问的是总个数,所以不需要考虑重复,只用考虑每一位上的贡献就行了。
将数字分成三部分,left(共i位),now和right(共j位)
如果当前now是0, 那么所有前i位是[0,left)的数字都+1个贡献,这些数一共有$left*10^j$个
如果当前now是[2,9],那么所有前i位是[0,left]的数字都+1个贡献,这些数一共有$(left+1)*10^j$个
如果当前now是1,那么这一位是1的数可以是,前i位为[0,left)的所有数,或是前i位刚好是left而后j位是[0,right]的。
一共有$left*10^j+right+1$个
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; LL n;
LL r; int main()
{
LL p = ;
cin>>n;
LL ans = ;
LL tmp = n;
while(n){
if(n % == ){
//cout<<n / 10 * p<<endl;
ans += n / * p;
}
else if(n % == ){
//cout<<(n / 10 * p) + r + 1<<endl;
ans += (n / * p) + r + ;
}
else{
//cout<<(n / 10 + 1) * p<<endl;
ans += (n / + ) * p;
} n /= ;
p *= ;
r = tmp % p;
} cout<<ans<<endl;
return ;
}
PAT甲级1049 Counting Ones【规律】的更多相关文章
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- PAT甲级——A1049 Counting Ones
The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- PAT 甲级 1004 Counting Leaves
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...
- PAT甲级 1004.Counting Leaves
参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...
- PAT甲级——A1115 Counting Nodes in a BST【30】
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT甲级——A1004 Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
随机推荐
- from中buttone 和 input type="button" 区别
在做一个表单提交时碰到的问题, 1.js判断阻止表单提交,如果是form 里面的button的话,恭喜你,你要再换个写法了.<button type="submit" ... ...
- having的用法
转载:http://blog.csdn.net/oathevil/article/details/5521757 where和having: “Where” 是一个约束声明,使用Where来约束来自于 ...
- GIt -- Window下配置 git
全局配置 git config --global user.name "账户名" git config --global use r.email '账户邮箱' 生成ssh,命令 ...
- 点击页面上的元素,页面删除removeChild()
简单描述:最近做了一个图片上传,上传完成回显图片的时候,需要用到点击图片,从页面删除的效果,然后就找到了removeChild()方法,说实话,我刚看到的时候,就觉得这个问题已经解决了,但是却发现这个 ...
- linux SWAP
1.内存和SWAP之间合理的分配方案 M = Amount of RAM in GB, and S = Amount of swap in GB, then If M < 2, S = M *2 ...
- SpringMVC的入门示例
1.配置流程说明 第一步:导入包 第二步:构建一个请求,编写请求页面 第三步:配置核心控制器 第四步:构建一个业务控制器 第五步:编写Spring配置文件 第六步:编写一个返回页面 2.配置流程--- ...
- 解决爬虫中遇到的js加密问题之有道登录js逆向解析
具体实现在github上面(有详细的步骤): https://github.com/WYL-BruceLong/Spider_JS_ReverseParsin
- [方案]基于Zynq WiFi方案构建
基于Zynq系列,搭建无线传输平台 1) 2.4G 2) 5G AC
- thinkphp5调用阿里大鱼短信
:在Controller.php public function send() { if (request()->isPost()) { $phone = input('post.phone/s ...
- IP网际协议
IP分类 IP地址分为网络号和主机号,5类不同的IP地址格式如下: A类地址每个网段内最多有224个,也就是16,777,214个. B类地址每个网段内最多有216个,也就是65535个. C类地址每 ...