Factorials (fact4)[USACO 3.2.1]

算法:模拟
 

分析:其实就是一道模拟题,直接用高精度求阶乘把末尾的0去掉就行了,因为我们要求的是第一个末尾不是0的个数,所以末尾的0有多少个与所求问题无关,直接忽略掉。

program fact4;

var
 n:longint;
 ans:ansistring;

function gjc(x,y:ansistring):ansistring;
var
 i,j,jinwei,lena,lenb,rlena,rlenb,len,t:longint;
 ts:ansistring;
 a,b,c:array [0..1000] of longint;
begin
 gjc:='';
 fillchar(c,sizeof(c),0);
 lena:=length(x) div 4;
 rlena:=length(x)-lena*4;
 lenb:=length(y) div 4;
 rlenb:=length(y)-lenb*4;
 for i:=1 to lena do val(copy(x,length(x)-4*i+1,4),a[i]);
 if copy(x,1,rlena)<>'' then
  begin
   inc(lena);
   val(copy(x,1,rlena),t);
   a[lena]:=t;
  end;
 for i:=1 to lenb do val(copy(y,length(y)-4*i+1,4),b[i]);
 if copy(y,1,rlenb)<>'' then
  begin
   inc(lenb);
   val(copy(y,1,rlenb),t);
   b[lenb]:=t;
  end;

 for i:=1 to lena do

  begin

   jinwei:=0;

   for j:=1 to lenb do

    begin

     jinwei:=a[i]*b[j]+jinwei div 10000+c[i+j-1];

     c[i+j-1]:=jinwei mod 10000;

    end;

   c[i+j]:=jinwei div 10000;

  end;

 len:=lena+lenb;

 while (c[len]=0) and (len>1) do dec(len);

 for i:=len downto 1 do

  begin

   str(c[i],ts);

   gjc:=gjc+ts;

  end;

end;



procedure main;

var

 i,j,st:longint;

 t:ansistring;

begin

 ans:='1';

 for i:=2 to n do

  begin

   str(i,t);

   ans:=gjc(ans,t);

   for j:=length(ans) downto 1 do if ans[j]<>'0' then break;

   if (j<>1) and (ans[j]<>'0') then delete(ans,j+1,length(ans)-j);

  end;

end;



begin

 assign(input,'fact4.in'); reset(input);

 assign(output,'fact4.out'); rewrite(output);



 readln(n);

 main;

 writeln(ans[length(ans)]);



 close(input); close(output);

end.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值