【PTA】1049 Counting Ones
The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12.
Input Specification:
Each input file contains one test case which gives the positive N (≤230).
Output Specification:
For each test case, print the number of 1's in one line.
Sample Input:
12
Sample Output:
5
Solution
数学
设\(N=d_nd_{n-1}d_{n-2}\cdots d_i \cdots d_2d_1\),对于每一个数位\(d_i\),计算当前数位可能出现1的次数并累加起来即可获得答案。
假设当前位为\(cur \rightarrow d_i\),记其左边的数\(left=d_nd_{n-1}d_{n-2}\cdots d_{i+1}\),右边的数为\(right=d_{i-1} \cdots d_3d_2d_1\)。使用一个变量\(a\)来记录当前位是个位,十位还是百位类推。根据数位的变化规律,有如下结论:
- \(cur==0\),\(ans+=left*a\)。当高位的数值从\(0\)变换到\(left-1\)时,在\(cur\)位处会出现\(left\)次1,因为对于一个数\(d_nd_{n-1}d_{n-2}\cdots d_i XXXX\)来说,前缀一旦确定,那么\(d_i\)只会在当前前缀的数字下出现一次。但是由于\(cur==0\),所以在以\(left\)为前缀的数中,\(right\)从\(0\)到\(999\cdots\),因此对于一个确定的\(left\),\(d_i\)出现1的次数有\(a\)次,因此一共有\(left*a\)次1出现。
- \(cur>1\),\(ans+=(left+1)*a\)。由于当前位的值大于1,因此高位的数值可以从\(0\)变换到\(left\),在\(cur\)位处会出现\(left+1\)次\(1\).
- \(cur==1\),\(ans+=left*a+right+1\),\(left*a\)的来源与\(cur==0\)的情况一致。由于\(cur==1\),因此当左侧前缀\(left\)取到最大时,右侧的后缀的范围不是从0~999...,而是从\(0\)到\(right(d_{i-1} \cdots d_3d_2d_1)\),因此这里还有\(right+1\)个1.
#include<bits/stdc++.h>
long long ans=0;
int main(){
int N;
std::cin>>N;
int left=0,right=0,cur=1,a=1;
int ans=0;
while(N/a){
left=N/(a*10);
right=N%a;
cur=N/a%10;
if(cur==0) ans+=left*a;
else if(cur==1) ans+=left*a+right+1;
else if(cur>1) ans+=(left+1)*a;
a*=10;
}
std::cout<<ans;
return 0;
}
【PTA】1049 Counting Ones的更多相关文章
- 【51NOD-0】1049 最大子段和
[算法]DP [题解]开long long…… #include<cstdio> #include<algorithm> #include<cstring> usi ...
- 【USACO2017JAN】 Promotion Counting
[题目链接] 点击打开链接 [算法] 离散化 + dfs + 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define ...
- 【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)
题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- 【hihoCoder】1049.后序遍历
问题:http://hihocoder.com/problemset/problem/1049?sid=767510 已知一棵二叉树的前序遍历及中序遍历结果,求后序遍历结果 思路: 前序:根-左子树- ...
- 【LeetCode】338. Counting Bits (2 solutions)
Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
- 【BZOJ】1049: [HAOI2006]数字序列(lis+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1049 题意:给一个长度为n的整数序列.把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希 ...
- 【wikioi】1049 棋盘染色(迭代深搜)
http://www.wikioi.com/problem/1049/ 这题我之前写没想到迭代加深,看了题解,然后学习了这种搜索(之前我写的某题也用过,,但是不懂专业名词 囧.) 迭代加深搜索就是限制 ...
- 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- 【SP26073】DIVCNT1 - Counting Divisors 题解
题目描述 定义 \(d(n)\) 为 \(n\) 的正因数的个数,比如 \(d(2) = 2, d(6) = 4\). 令 $ S_1(n) = \sum_{i=1}^n d(i) $ 给定 \(n\ ...
- 【计数】【UVA11401】 Triangle Counting
传送门 Description 把1……n这n个数中任取3个数,求能组成一个三角形的方案个数 Input 多组数据,对于每组数据,包括: 一行一个数i,代表前i个数. 输入结束标识为i<3. O ...
随机推荐
- 打开IE浏览器被强制跳转至edge
打开edge浏览器,右上角更多,设置,默认浏览器,设置"从不" 控制面板,Internet选项,高级,浏览,取消勾选"启用第三方浏览器扩展"
- 使用shell定时执行脚本
yum install crontabs //安装 #检查安装crontab -V #服务操作service crond start //启动服务service crond stop //关闭服务se ...
- 【运维】通过gotty实现网页代理访问服务器及K8S容器操作实践
Gotty 是Golang编写的可以方便的共享系统终端为web应用,是一个灵活强大的通过web访问终端的工具.本文将主要通过搭建Gotty实现对K8S容器的访问操作,开发如果想要正常的进行容器访问以及 ...
- docker systemctl start报错: Failed to get D-Bus connection: Operation not permitted
转载自:https://blog.csdn.net/zhenliang8/article/details/78330658 最近使用docker部署ansible,安装ssh 遇到启动服务报错:Fai ...
- [Oracle19C 数据库管理] 用户与权限管理
用户管理 用户具有以下属性: 用户名: 不能超过30位.不能包含特殊字符.必须用字符开头.用户名不区分大小写. 认证方式: 最常见的是密码认证. 默认永久表空间: 控制用户可以在哪个表空间里创建对象. ...
- SCI论文写作技巧-introduction和related works
introduction怎么写 a)背景介绍,现状(介绍别人研究),存在问题,怎样解决,我的做法,有何亮点 b)研究背景和重要性.引出该领域科研空白.点题-指出本文的研究课题.概述文章的核心方法论和主 ...
- vue组件之间的传参
vue组件之间传参有三种传参方式'父传子','子传父','非父子组件之间传值' 父传子 父组件 <template> <CounterCom :num="5"&g ...
- centos7 添加自定义程序为系统服务
centos6版本的系统服务是/etc/init.d启动脚本的方式,centos7采用强大的systemctl来管理系统服务,大幅提供了系统服务的运行效率,但是服务的配置和以前版本完全不同,这是很大的 ...
- c语言学习--静态函数
静态函数 #include<stdio.h> //这是静态函数, 静态函数只能在当前文件调用,其他文件下面的函数是没法调用到这个函数的 static void fun1() { print ...
- C语言中return和exit的区别
转载自:http://jszx.cuit.edu.cn/NewsCont.asp?bm=00&type=888&id=20050 1.exit用于在程序运行的过程中随时结束.终止程序, ...