计数问题

题意

试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?


分析

例如,在 1 到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。


var
n,x,i,tj,j:longint;
zfc,zfx:string;
begin
    read(n,x);
    tj:=0;str(x,zfx);
    for i:=1 to n do
    begin
        str(i,zfc);
        if pos(zfx,zfc)>=1 then
        for j:=1 to length(zfc) do
        if zfc[j]=zfx then inc(tj);
    end;
    write(tj);
end.


posted @ 2017-02-23 15:48  银叶草  阅读(90)  评论(0编辑  收藏  举报
Live2D