1049.(*) Counting Ones
题意:题目大意:给出一个数字n,求1~n的所有数字里面出现1的个数
思路:转自(柳婼 の blog)遍历数字的低位到高位,设now为当前位的数字,left为now左边的所有数字构成的数字,right是now右边的所有数字构成的数字。只需一次次累加对于当前位now来说可能出现1的个数,然后把它们累加即可。a表示当前的个位为1,十位为10,百位为100类推。
对于now,有三种情况:
- 1.now == 0 : 那么 ans += left * a;
- 2.now == 1 : ans += left * a + right + 1;
- 3.now >= 2 : ans += (left + 1) * a;
从排列组合的角度分析会显得容易些。比如now==1时,当前位出现1时,左边的数字可能小于left也可能等于left。
- 1、小于left时,左边共left种可能,now右边的数字是任意的,共有a种可能,a=(10^now右边的数的位数)。因为now左边小于left,无论now右边的数字如何,整个数字一定属于1~n范围的
- 2、等于left时,左边只有一种可能,now的数不能是任意的了,只能在0~right变化。
以上两种情况相加,即now为1时,共left * a + right + 1种
代码:
#include <iostream>
using namespace std;
int main() {
int n, left = , right = , a = , now = , ans = ;
scanf("%d", &n);
while(n / a) {
left = n / (a * ), now = n / a % , right = n % a;
if(now == ) ans += left * a;
else if(now == ) ans += left * a + right + ;
else ans += (left + ) * a;
a = a * ;
}
printf("%d", ans);
return ;
}
1049.(*) Counting Ones的更多相关文章
- (Problem 73)Counting fractions in a range
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- 深入理解JVM(③)各种垃圾收集算法
前言 从如何判定对象消亡的角度出发,垃圾收集算法可以划分为"引用计数式垃圾收集"(Reference Counting GC)和"追踪式垃圾收集"(Tracin ...
- 以下C#程序的输出结果是( )。
以下程序的输出结果是( ). using System; namespace HoverTreeTikuConsole { class Program { static void Main(strin ...
- PHP 位运算(&, |, ^, ~, <<, >>)及 PHP错误级别报告设置(error_reporting) 详解
位运算符允许对整型数中指定的位进行求值和操作. 位运算符 例子 名称 结果 $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1. $a | $b Or(按位或) ...
- 页面加载完成后,触发事件——trigger()
<button id="btn">点击我</button> <div id="test"></div> 如果页面 ...
- linux/unix 编程手册 fork()函数
父进程通过fork()函数创建子进程,将父进程数据段和栈的内容拷贝到子进程中,子进程执行程序execve创建新程序,调用exit函数退出到等待wait(),挂起父进程, 父子进程享用相同的程序文本段. ...
- setInterval()与clearInterval()的一个有趣小现象
今天在使用setInterval()时,发现了一个有意思的事情 代码如下: var box=document.getElementById("box");//获取id为“box”的 ...
- HTML DOM对象之createElement()方法
今天在学习DOM节点操作时,发现了创建DOM节点的createElement()方法的一个有意思的现象. 代码如下: var box=document.getElementById("box ...
随机推荐
- [LeetCode&Python] Problem 830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [LeetCode&Python] Problem 771: Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- Cocos2dx 中的点击事件
简单记录一下2dx的鼠标交互事件.以及精灵绑定盒的点击判定 Layer 子类的 init方法中: auto listener = EventListenerTouchOneByOne::cre ...
- CodeForces - 710F:String Set Queries (二进制分组 处理 在线AC自动机)
ou should process m queries over a set D of strings. Each query is one of three kinds: Add a string ...
- s21day01 python笔记
s21day01 python笔记 一.计算机基础 计算机的初步认识 用户:人 软件:QQ.浏览器等 解释器/编译器/虚拟机:java解释器.python解释器等 操作系统 硬件:CPU.内存.硬盘. ...
- python------模块定义、导入、优化 ------->random模块
2.random模块 #随机浮点数 random.random() #生成0到1之间的随机浮点数,不能自己指定 random.uniform(1,10) #可以指定 #随机整数 random. ...
- nginx实现反向代理,以反向代理tomcat为例
我的nginx和tomcat在同一台服务器上 我nginx安装的位置(因为我安装时使用的是./configure --prefix=/usr/etc/nginx)是/usr/etc/nginx,进入安 ...
- nginx日志格式配置
我一向对日志这个东西有些许恐惧,因为在分析日志是需要记住不同服务器日志的格式,就拿提取ip这一项来说,有的服务器日志是在第一列,有的是第二列或则第三列等等.知道今天我才发现,日志格式是可以自定义配置的 ...
- Nginx配置基于ip的虚拟主机
我是在centos7虚拟机上进行实验的 该实验基于添加好ip的基础上,如何给网卡添加ip请查阅我的博客 先来看一下我的ip [root@localhost nginx]# ifconfig ens33 ...
- 试用 openresty/lua-resty-shell
openresty/lua-resty-shell 是当前最新rc 版本内置的shell 功能,我们可以用来执行一个脚本,以及命令 还是比较方便的. 测试集成了一个oreilly电子书下载的功能 环境 ...