[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个,现在需要统计这些自然数各自出现的次数,并按照 ...
随机推荐
- 源码方式安装mysql5.5
mysql5.5开始,源码配置编译工具configure变成了cmake,所以先要去把cmake装上.并安装make,bison,cmake,gcc-c++,ncurses的包 去http://www ...
- Effective C++ 之 Item 5:了解C++默默编写并调用哪些函数
Effective C++ chapter 2. 构造 / 析构 / 赋值运算 (Constructors, Destructors, and Assignment Operators) Item 5 ...
- Vector[C++]
// vector<int> vec; // for(int i = 0; i < 10; i++) // { // vec.push_back(5) ...
- 制作U盘启动系统盘
下载ULtraISO,安装之后,先打开一个iso系统文件,然后选中菜单“启动”下的“写入硬盘映像”
- ubuntu中禁用华硕S550C触摸屏的方法
华硕S550C的触摸屏被我一不小心弄了一条裂缝,导致屏幕一直会莫名其妙自动进行点击,严重影响了使用.在windows 系统下通过FN+F7的快捷键可以直接禁用触摸屏,但是换成ubuntu 系统之后,快 ...
- 在Salesforce中将 Decimal 数据转换成美元格式
闲言少叙,直接上代码(Apex Class 中的方法): private string ConvertToMoneyFormat(decimal price){ if (price == null | ...
- 用PowerShell脚本删除SharePoint 的 Page中的WebPart
编写PowerShell脚本可以删除page中所有的webpart,也可以根据webpart的属性信息去删除特定的webpart. 下面的PowerShell脚本便是删除对应page中所有的webpa ...
- loj 1032 数位dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1032 思路:数位dp, 采用记忆化搜索, dp[pos][pre][have] 表示 ...
- (转载)如何借助KeePassX在Linux上管理多个密码
转自:http://netsecurity.51cto.com/art/201311/417764.htm 如今,基于密码的身份验证在网上非常普遍,结果你恐怕数不清自己到底在使用多少个密码.实际上,据 ...
- 【SSM】拦截器的原理、实现
一.背景: 走过了双11,我们又迎来了黑色星期五,刚过了黑五,双12又将到来.不管剁手的没有剁手的,估计这次都要剁手了!虽然作为程序猿的我,没有钱但是我们长眼睛了,我们关注到的是我们天猫.淘宝.支付宝 ...