题目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【规律】的更多相关文章

  1. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...

  2. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  3. pat 甲级 1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...

  4. PAT甲级——A1049 Counting Ones

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

  5. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

  6. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  7. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  8. 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 ...

  9. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

随机推荐

  1. DDoS的类型及原理

    1.DDoS攻击: DDOS(Distributed Denial of Service),又称分布式拒绝服务攻击.骇客通过控制多个肉鸡或服务器组成的僵尸网络,对目标发送大量看似合法请求,从而占用大量 ...

  2. Ubuntu18.04 VMtools的安装与卸载

    VM不推荐在Ubuntu中使用VMtools而是open-vm-tools,原文地址https://kb.vmware.com/s/article/2073803 安装方式 1 更新系统源 sudo ...

  3. ActiveMQ依赖JDK版本关系

    1.如何查看官方发布的activeMQ依赖的JDK版本1)以ActiveMQ 5.15.2 Release为例:在下载页面的Change Log处, 2)打开下载号的jar包,以activemq-al ...

  4. 帆软报表(finereport)动态列查询

    新建普通报表,新建一个数据集ds1,sql语句:select * from 销量 设计模板:选择复选按钮组控件 设置  控件名称 paraed , 控件值设置为公式:["列名",& ...

  5. ROS-十步完成ROS-indigo安装

    Ubuntu 版本是14.04.5.(这个版本的ubuntu的内核是V4.4的内核,长期维护到2019年,是14代比较稳定的,反正用这个安装就没有什么依赖的头疼的问题,安装ROS很方便,可以去清华的网 ...

  6. 【原创】大叔经验分享(3)hbase client 如何选择

    java中访问hbase有两种方式,一种是hbase自带的client,一种是通过hbase thrift 1 hbase client示例 Configuration conf = HBaseCon ...

  7. 快速搭建一个Spring Boot + MyBatis的开发框架

    前言:Spring Boot的自动化配置确实非常强大,为了方便大家把项目迁移到Spring Boot,特意总结了一下如何快速搭建一个Spring Boot + MyBatis的简易文档,下面是简单的步 ...

  8. Python-form表单标签

    语义:标记表单 #1.什么是表单? 表单就是专门用来接收用户输入或采集用户信息的 #2.表单的格式 <form> <表单元素> </form> 链接:https:/ ...

  9. .net Core 目录浏览权限

    StartUp 类库中 ConfigureServices:方法中增加文件夹浏览服务. Configure: 添加中间件 1.app.UseStaticFiles() 2.//增加文件访问权限app. ...

  10. H5测试与PC端测试不同的点

    1.通过H5网页(非手机的返回功能)的返回功能可以返回,不会出现无法返回的情况. 2.横屏竖屏相互切换,能自适应,并且布局不会乱掉: 3.为能在不同分辨率的手机上能更好的展示,建议采用响应式设计(如: ...