题目概述
试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1
到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。
解题思路
本题直接将每个数的每一位抠出来比较就能过,但我们现在讨论另一种做法。
对于个位上的数字,每10个数中出现1次;
对于十位上的数字,每100个数中出现10次;
以此类推……
对于x=0的情况要进行特殊处理:
当10<=n<=99时,0的出现被算多了00,01,02,03,04,05,06,07,08,09这几个数,一共多出了11次;
当100<=n<=999时,同理,0被多算了111次;
……
将被多算的部分减去即可。
时间复杂度:O(log n)
空间复杂度:O(n)
源程序
const
b:array[1..9]of longint=(1,10,100,1000,10000,100000,1000000,10000000,100000000);
var
i,n,a,x,t,p:longint;
begin
readln(n,x);
t:=n;
i:=1;
while t>0 do
begin
a:=a+t div 10*b[i];
t:=t div 10;
inc(i);
end;
t:=n;
i:=1;
while t>0 do
begin
p:=t mod 10;
if p>x then a:=a+b[i];
if (p=x)and(i>=2) then a:=a+n mod b[i]+1;
if (p=x)and(i=1) then a:=a+1;
t:=t div 10;
inc(i);
end;
if x=0 then begin
p:=1;
t:=n div 10;
while t>0 do
begin
p:=p*10+1;
t:=t div 10;
end;
a:=a-p;
end;
write(a);
end.