[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个,现在需要统计这些自然数各自出现的次数,并按照 ...
随机推荐
- document.body.scrollTop
标准浏览器:document.documentElement.scrollTop; 谷歌浏览器:document.body.scrollTop; var scrollTop = document.do ...
- 【前台 】字符串和js对象的相互转化
利用原生JSON对象,将对象转为字符串 var jsObj = {}; jsObj.testArray = [1,2,3,4,5]; jsObj.name = 'CSS3'; jsObj.date = ...
- vim 清空
插入模式 首先执行gg 跳至文件首行 然后执行dG就清空了整个文件 还有一种方法就要退出VIM,然后使用echo > file ,这样也能快速清空文件内容
- 基线 css
原文地址:http://blog.jobbole.com/31926/ 英文原文:CSS Baseline,编译:飞鸟分享 译者注:网页设计布局中一直比较流行网格对齐,但只是针对水平的对齐,很少或者没 ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- 20145223《Java程序程序设计》第10周学习总结
20145223<Java网络编程> 一.Java的网络编程 ·网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. ·java.net包中J2SE的API包含有类 ...
- event 内存泄漏
组长说用event有内存泄漏的隐患..做个测试. 预留
- 使用Gson排除特定字段
http://blog.csdn.net/hknock/article/details/51037564
- POJ1637 Sightseeing tour(判定混合图欧拉回路)
有向连通图存在欧拉回路的充要条件是所有点入度=出度. 首先随便给定所有无向边一个方向(不妨直接是u->v方向),记录所有点的度(记:度=入度-出度). 这时如果有点的度不等于0,那么就不存在欧拉 ...
- (转)hbase master挂掉-zookeeper连接超时原因
link:http://www.51testing.com/?uid-445759-action-viewspace-itemid-812467 并行运行hbase删表,建表操作,多个表多个regio ...