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 ...
随机推荐
- Javascript arguments.callee和caller的区别
一.callee 在学习callee之前,需要先学习arguments. arguments: 含义:该对象代表正在执行的函数和调用它的函数的参数. 语法: 1 [function.]argument ...
- javascript基础 之 保留关键字
1,保留关键字 意思是:特定的字符串要么是已经有指代了要么是未来将要有指代,所以取名字不要用保留关键字里的字符串 js保留关键字 abstract arguments boolean break by ...
- 一个基于angularJS的工资计算器
先看界面: 其实在ng中最让人印象深刻的就是数据的双向绑定,在html中就完成了很多操作.大概用到的就是控制器视图服务等,没有分模块写控制器,代码如下: <html ng-app = " ...
- docker镜像的使用及相关
参考网站docker中文网:http://www.docker.org.cn/book/docker/docker-push-image-13.html 1.搜索容器: docker search t ...
- 十.nginx反向代理负载均衡服务实践部署
期中集群架构-第十章-nginx反向代理负载均衡章节章节====================================================================== 0 ...
- (转发)Navicat 远程连接 Linux服务器上的MySQL数据库
授权法: 在安装mysql的机器上运行: 1.# ./bin/mysql -uroot -p //这样应该可以进入MySQL服务器 2.mysql> GRANT ALL PRIVILEGES O ...
- Windows Internals 笔记——字符和字符串处理
1.自Windows NT起,Windows的所有版本都完全用Unicode来构建,调用Windows函数时,如果向它传入一个ANSI字符串,那么函数首先会把字符串转换为Unicode,再把结果传给操 ...
- python内置模块之-logging
logging 模块主要用于写日志 logging模块主要有如下几个组件 Logger Logger对象提供应用程序可直接使用的接口 Handler Handler发送日志到适当的目的地 Filte ...
- javascript 通过模块模式实现代码访问控制
<script type="text/javascript"> // 在匿名函数中使用var关键字 (function(){ var privateField = 42 ...
- 【国庆】记一次mysqld_safe引发mysql进程故障
今天是举国欢庆的日子,但是Mariadb密码忘记了,于是巴拉巴拉的执行"mysqld_safe --skip-grant-tables &"这个神技能,打算跳过密码验证,直 ...