首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。 例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。
输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。 例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。
admin
2019-03-29
136
问题
输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。
例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。
选项
答案
#include "string.h" #include "stdlib.h" int NumberOf1(const char* strN); int PowerBase10(unsigned int n); ///////////////////////////////////////////////////////////////////////////// // Find the number of 1 in an integer with radix 10 // Input: n - an integer // Output: the number of 1 in n with radix ///////////////////////////////////////////////////////////////////////////// int NumberOf1BeforeBetween1AndN_Solution2(int n) { if(n <= 0) return 0; // convert the integer into a string char strN[50]; sprintf(strN, "%d", n); return NumberOf1(strN); } ///////////////////////////////////////////////////////////////////////////// // Find the number of 1 in an integer with radix 10 // Input: strN - a string, which represents an integer // Output: the number of 1 in n with radix ///////////////////////////////////////////////////////////////////////////// int NumberOf1(const char* strN) { if(!strN || *strN < ’0’ || *strN > ’9’ || *strN == ’\0’) return 0; int firstDigit = *strN - ’0’; unsigned int length = static_cast
(strlen(strN)); // the integer contains only one digit if(length == 1 && firstDigit == 0) return 0; if(length == 1 && firstDigit > 0) return 1; // suppose the integer is 21345 // numFirstDigit is the number of 1 of 10000-19999 due to the first digit int numFirstDigit = 0; // numOtherDigits is the number of 1 01346-21345 due to all digits // except the first one int numOtherDigits = firstDigit * (length - 1) * PowerBase10(length - 2); // numRecursive is the number of 1 of integer 1345 int numRecursive = NumberOf1(strN + 1); // if the first digit is greater than 1, suppose in integer 21345 // number of 1 due to the first digit is 10^4. It’s 10000-19999 if(firstDigit > 1) numFirstDigit = PowerBase10(length - 1); // if the first digit equals to 1, suppose in integer 12345 // number of 1 due to the first digit is 2346. It’s 10000-12345 else if(firstDigit == 1) numFirstDigit = atoi(strN + 1) + 1; return numFirstDigit + numOtherDigits + numRecursive; } ///////////////////////////////////////////////////////////////////////////// // Calculate 10^n ///////////////////////////////////////////////////////////////////////////// int PowerBase10(unsigned int n) { int result = 1; for(unsigned int i = 0; i < n; ++ i) result *= 10; return result; }
解析
这是一道广为流传的google面试题。用最直观的方法求解并不是很难,但遗憾的是效率不是很高;而要得出一个效率较高的算法,需要比较强的分析能力,并不是件很容易的事情。当然,google的面试题中简单的也没有几道。
首先我们来看最直观的方法,分别求得1到n中每个整数中1出现的次数。而求一个整数的十进制表示中1出现的次数,就和本面试题系列的第22题很相像了。我们每次判断整数的个位数字是不是1。如果这个数字大于10,除以10之后再判断个位数字是不是1。基于这个思路,不难写出如下的代码:
int NumberOf1(unsigned int n);
/////////////////////////////////////////////////////////////////////////////
// Find the number of 1 in the integers between 1 and n
// Input: n - an integer
// Output: the number of 1 in the integers between 1 and n
/////////////////////////////////////////////////////////////////////////////
int NumberOf1BeforeBetween1AndN_Solution1(unsigned int n)
{
int number = 0;
// Find the number of 1 in each integer between 1 and n
for(unsigned int i = 1; i <= n; ++ i)
number += NumberOf1(i);
return number;
}
/////////////////////////////////////////////////////////////////////////////
// Find the number of 1 in an integer with radix 10
// Input: n - an integer
// Output: the number of 1 in n with radix
/////////////////////////////////////////////////////////////////////////////
int NumberOf1(unsigned int n)
{
int number = 0;
while(n)
{
if(n % 10 == 1)
number ++;
n = n / 10;
}
return number;
}
这个思路有一个非常明显的缺点就是每个数字都要计算1在该数字中出现的次数,因此时间复杂度是O(n)。当输入的n非常大的时候,需要大量的计算,运算效率很低。我们试着找出一些规律,来避免不必要的计算。
我们用一个稍微大一点的数字21345作为例子来分析。我们把从1到21345的所有数字分成两段,即1-1235和1346-21345。
先来看1346-21345中1出现的次数。1的出现分为两种情况:一种情况是1出现在最高位(万位)。从1到21345的数字中,1出现在10000-19999这10000个数字的万位中,一共出现了10000(10
4
)次;另外一种情况是1出现在除了最高位之外的其他位中。例子中1346-21345,这20000个数字中后面四位中1出现的次数是2000次(2*10
3
,其中2的第一位的数值,10
3
是因为数字的后四位数字其中一位为1,其余的三位数字可以在0到9这10个数字任意选择,由排列组合可以得出总次数是2*10
3
)。
至于从1到1345的所有数字中1出现的次数,我们就可以用递归地求得了。这也是我们为什么要把1-21345分为1-1235和1346-21345两段的原因。因为把21345的最高位去掉就得到1345,便于我们采用递归的思路。
分析到这里还有一种特殊情况需要注意:前面我们举例子是最高位是一个比1大的数字,此时最高位1出现的次数10
4
(对五位数而言)。但如果最高位是1呢?比如输入12345,从10000到12345这些数字中,1在万位出现的次数就不是10
4
次,而是2346次了,也就是除去最高位数字之后剩下的数字再加上1。
基于前面的分析,我们可以写出以下的代码。在参考代码中,为了编程方便,我把数字转换成字符串了。
转载请注明原文地址:https://kaotiyun.com/show/4xmZ777K
0
程序员面试
相关试题推荐
YourfriendDavidjustsentyouabookthatyouexpectedasabirthdaypresent.Sendane-mailtohimtoexpressyourappreciati
WhenIseeclients,thisisthequestionthatI’maskedthemost.Ifyou’reinapublicplace,lookaround.【F1】Nearlyeveryone
Publicationbiasinacademicjournalsisnothingnew.Afindingofnocorrelationbetweensportingeventsandeitherviolentcri
输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。例如输入整数22和如下二元树10
输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:因此返回true。如果输入7、4、6、5,没有哪棵树的后序遍历
在c#中using和new这两个关键字有什么意义,请写出你所知道的意义?
从当前界面上的菜单或“网络任务”开始创建拨号连接,通过Modem连接到In-ternet,拨号时先拨0,再拨16300,用户名和密码均为16300,将创建的连接的名称命名为:linkl,然后在桌面上创建一个到此连接的快捷方式。除此之外,其余选项均使用默认设
利用快捷方式向导,在桌面上为文件夹“C:\office”中的应用程序“EXCEL.EXE”创建名为“EXCEL”的快捷方式。
在Excel中,公式SUM(C2:C6)的作用是()。A.求C2到C6这五个单元格数据之和B.求C2和C6这两个单元格数据之和C.求C2和C6两单元格的比值D.以上说法都不对
以下哪个配置将VLAN25应用于快速以太网接口0/3?A、conftinterfacef0/1switehportmodetrunkvlan25B、conftinterfacef0/3swi
随机试题
学习策略
哮喘发作与食物有关因素
患者,男,32岁。3日来头痛如裹,痛无休止,肢体困重,苔白腻,脉濡。针刺治疗的配穴为
某小学有大批的学生发生不明原因的腹泻,为了寻找病因及流行的线索。通过第一步的研究,结果提示大批学生的腹泻可能与饮用了某厂生产的饮料有关。下一步最好采取
初产妇,妊娠38周。合并心脏病已临产。心率100次/分,心功能Ⅲ级,骨盆测量正常。宫口开大5cm,正枕前位,先露S+1。最适宜的分娩方式是
某演出公司进口舞台设备一套,实付金额折合人民币185万元,其中包含单独列出的进口后设备安装费10万元、中介经纪费5万元;运输保险费无法确定,海关按同类货物同期同程运输费计算的运费25万元。假定关税税率20%,该公司进口舞台设备应缴纳的关税为()。
在文学走出去的过程中,用______的画面表现故事,可以使不同喜好的观众在______文字、图像、声音的立体式信息空间中,进行文学的超时空阅读和赏析。填入划横线部分最恰当的一项是:
宋代书院在教学和管理方面有哪些重要特点?
直线x=2,y=2与圆(x-1)2+(y-1)2=1分别相切于A、B两点,与劣弧AB相切于该劣弧中点的直线方程为
Manyteachersbelievethattheresponsibilitiesforlearningliewiththestudent.【C1】______alongreadingassignmentisgiven,
最新回复
(
0
)