题意:题目大意:给出一个数字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的更多相关文章

  1. (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 ...

  2. (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 ...

  3. 深入理解JVM(③)各种垃圾收集算法

    前言 从如何判定对象消亡的角度出发,垃圾收集算法可以划分为"引用计数式垃圾收集"(Reference Counting GC)和"追踪式垃圾收集"(Tracin ...

  4. 以下C#程序的输出结果是( )。

    以下程序的输出结果是( ). using System; namespace HoverTreeTikuConsole { class Program { static void Main(strin ...

  5. PHP 位运算(&, |, ^, ~, <<, >>)及 PHP错误级别报告设置(error_reporting) 详解

    位运算符允许对整型数中指定的位进行求值和操作. 位运算符 例子 名称 结果 $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1. $a | $b Or(按位或) ...

  6. 页面加载完成后,触发事件——trigger()

    <button id="btn">点击我</button> <div id="test"></div> 如果页面 ...

  7. linux/unix 编程手册 fork()函数

    父进程通过fork()函数创建子进程,将父进程数据段和栈的内容拷贝到子进程中,子进程执行程序execve创建新程序,调用exit函数退出到等待wait(),挂起父进程, 父子进程享用相同的程序文本段. ...

  8. setInterval()与clearInterval()的一个有趣小现象

    今天在使用setInterval()时,发现了一个有意思的事情 代码如下: var box=document.getElementById("box");//获取id为“box”的 ...

  9. HTML DOM对象之createElement()方法

    今天在学习DOM节点操作时,发现了创建DOM节点的createElement()方法的一个有意思的现象. 代码如下: var box=document.getElementById("box ...

随机推荐

  1. [LeetCode&Python] Problem 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  2. golang channel 总结

    1.未初始化的channel读,阻塞 package main import ( "fmt" "time" ) func main() { var ch cha ...

  3. Blender 插件整理

    系统自带插件列表: 好用的第三方插件: Align Vertices to Grease Pencil, 对齐顶点到蜡笔,   https://blenderartists.org/t/addon-a ...

  4. 【HDOJ3018】【一笔画问题】【欧拉回路+并查集】

    http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Me ...

  5. 100 webhook implementations

    转自: https://streamdata.io/blog/100-webhook-implementations/  很不错的整理 What is the scope of the event-d ...

  6. PowerDesigner基础使用 ---- 入门学习

    1:入门级使用PowerDesigner软件创建数据库(直接上图怎么创建,其他的概念知识可自行学习) 我的PowerDesigner版本是16.5的,如若版本不一样,请自行参考学习即可.(打开软件即是 ...

  7. node api 之:util

    util 库的使用: const util = require('util'); util 的方法: 方法 含义 util.inherits(constructor, superConstructor ...

  8. git在不同平台windows、linux、mac 上换行符的问题

    0.01 不同平台上换行符的问题 1,不同平台对换行符的制定不同 windows <回车换行> (carriage return AND line feed) “\n\r” CRLF Un ...

  9. tcpdump过滤某个端口

    一般我们使用Tcpdump时都是使用: Java代码   tcpdump -i ethx      www.2cto.com   下面这条命令就是查看80端口的访问量,进行排序,取前20位    Ja ...

  10. laravel 获取上一条insert语句产生的id

    <?php //頭部引入DB類 use Illuminate\Support\Facades\DB; //在方法中獲取获取上一条insert语句产生的id $id = DB::getPdo()- ...