[CareerCup] 18.4 Count Number of Two 统计数字2的个数
18.4 Write a method to count the number of 2s between 0 and n.
这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可。LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处。那么首先这题可以用brute force来解,我们对区间内的每一个数字都调用一个函数,用来统计该数字中2出现的个数。而统计一个数字各位上而出现的个数很简单,就是平移位,对10取余,如果为2,则计数器自增1,然后此数自除以10,直至为0停止,参见代码如下:
解法一:
int count_number_2(int num) {
int res = ;
while (num > ) {
if (num % == ) ++res;
num /= ;
}
return res;
}
int count_in_range(int num) {
int res = ;
for (int i = ; i <= num; ++i) {
res += count_number_2(i);
}
return res;
}
其实这道题还有更好的办法,我们不是在区间里一个数一个数的找2,而是按位来找,比如我们先来列出一部分序列:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
...
110 111 112 113 114 115 116 117 118 119
我们发现最低位出现2的频率是每10个数字出现1个,我们大概可以分为三种情况来讨论,digit < 2, digti = 2, 和digit > 2。
当digit < 2时,例如x=61523, d=3, 那么x[d]=1,从低位开始坐标为3的数字是1(从0开始),那么第三位上的2出现于2000-2999, 12000-12999, 22000-22999, 32000-32999, 42000-42999, 52000-52999, 所以总共有6000个2,在第三位上。怎么跟我们在区间[1,60000]上只计算第三位上的2的个数相同。
当digit > 2时,大于2的情况跟上面的分析方法相同,比如x=63523,那么这跟区间[0,70000]上第三位是2的个数相同,所以我们rounding up一下即可。
当digit = 2时,比如x=62523,我们知道[0,61999]区间的第三位2的个数可以通过上面第一种情况计算出来,那么怎么计算[62000,62523]区间中第三位2的个数呢,共有524个,用62523-62000+1即可。
根据上述分析,我们不难写出代码如下:
解法二:
int count_in_range_as_digit(int num, int d) {
int power_of_10 = pow(, d);
int next_power_of_10 = power_of_10 * ;
int right = num % power_of_10;
int round_down = num - num % next_power_of_10;
int round_up = round_down + next_power_of_10;
int digit = (num / power_of_10) % ;
if (digit < ) return round_down / ;
else if (digit == ) return round_down / + right + ;
else return round_up / ;
}
int count_in_range(int num) {
int res = ;
int len = to_string(num).size();
for (int i = ; i < len; ++i) {
res += count_in_range_as_digit(num, i);
}
return res;
}
[CareerCup] 18.4 Count Number of Two 统计数字2的个数的更多相关文章
- [CareerCup] 18.3 Randomly Generate Integers 随机生成数字
18.3 Write a method to randomly generate a set of m integers from an array of size n. Each element m ...
- [LeetCode] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 233 Number of Digit One 数字1的个数
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetc ...
- PHP统计二维数组个数
count($arr) $arr = [ ['id'=>1,'name'=>'Tom'], ['id'=>2,'name'=>'Sun'], ['id'=>3,'name ...
- 1093. Count PAT’s (25)-统计字符串中PAT出现的个数
如题,统计PAT出现的个数,注意PAT不一定要相邻,看题目给的例子就知道了. num1代表目前为止P出现的个数,num12代表目前为止PA出现的个数,num123代表目前为止PAT出现的个数. 遇到P ...
- lintcode 中等题:digits counts 统计数字
题目 统计数字 计算数字k在0到n中的出现的次数,k可能是0~9的一个值 样例 例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现 ...
- NOIP2007 统计数字
1.统计数字 (count.pas/c/cpp) [问题描述] 某次科研调查时得到了 n 个自然数,每个数均不超过 1500000000(1.5*109).已知不相同的数 不超过 10000 个,现在 ...
- 给定任意一个字符串,使用 for in 语句来统计字符出现的个数
//找出字符串中的数字 var str = 'haj123sdk54hask33dkhalsd879'; /*function findNum(str){ var arr = []; var tmp ...
- 洛谷-统计数字-NOIP2007提高组复赛
题目描述 Description 某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*10^9).已知不相同的数不超过10000个,现在需要统计这些自然数各自出现的次数,并按照 ...
随机推荐
- SQLite密码添加移除
背景:电脑清理--个人洁癖 SQLite的最原始的是没有加密的,从而衍生了多种加密算法,但在平常使用中使用System.Data.Sqlite,但其加密后,一般都需要要单独的sqlite管理器--像我 ...
- WPF之MVVM(Step2)——自己实现DelegateCommand:ICommand
在自己实现MVVM时,上一篇的实现方式基本是不用,因其对于命令的处理不够方便,没写一个命令都需要另加一个Command的类.此篇主要介绍DelegateCommand来解决上面所遇到的问题. 首先,我 ...
- 人性的弱点&&影响力
How wo win friends and influence people 人性的弱点 by 卡耐基 人际关系基本技巧 不要批评.谴责.抱怨 真诚的欣赏他人 激发他人的渴望 获得别人好感的方式 微 ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- 智能车学习(十六)——CCD学习
一.使用硬件 1.兰宙CCD四代 优点:可以调节运放来改变放大倍数 缺点:使用软排线(容易坏),CCD容易起灰,需要多次调节 2.野火K60底层 二.CCD硬件电路 ( ...
- Linux学习笔记(8)Linux常用命令之网络命令
(1)write write命令用于给指定用户发信息,以Ctrl+D保存结束,所在路径为/usr/bin/write,其语法格式为: write [用户名] 注:只能给在线用户发送. 例:新建ws用户 ...
- C语言字符串操作常用库函数
C语言字符串操作常用库函数 *********************************************************************************** 函数 ...
- td 的colspan属性
看来要长长记性了,这个问题上次遇到过这次又犯了这个错. <table> <tr> <td colspan="10"> </td> & ...
- 制作一个访问量很高的大型网站,你会如何来管理所有HTML、CSS、JS与图片?
1.先确定全局样式和编码方式. 2.编写习惯,标注样式. 3.文件夹存放,命名.
- BZOJ 3053 The Closest M Points
[题目分析] 典型的KD-Tree例题,求k维空间中的最近点对,只需要在判断的过程中加上一个优先队列,就可以了. [代码] #include <cstdio> #include <c ...