【NOIP提高组】就是乘法

Description

这一天富爷又来找大头玩乘法游戏,然而不同于富爷的口算能力,大头只能列下了式子。第一题是432 × 5678:
432
5678
-------
3456
3024
2592
2160
-------
2452896
作为环保主义者的大头,认为最后一行的答案一定不能有任何的前导空格,当然了,对于某些行来说前导空格不能省。但是,珍惜资源的大头,认为任何一行都不能有任何的后导空格。作为完美主义者的大头,认为每一行的长度都应该和最后的答案行匹配。
啊,对了,有时大头还会遇到富爷的坑,假如第二个乘数有一个位置是0的话,你会发现一个0就是对应行的答案,那就把0挪到下一行,并省去这一行~~举个例子,对于200001 × 90040:

              200001
                 90040

-----------
8000040
180000900
-----------
18008090040

第四行的最右边是一个0,就是前一行(已省去)挪下来的,而第五行的最后两个0,则是由前两行(已省去)挪下来的。
最后一个坑,对于那些只有一行中间答案的,那么最后一部分也可以省略,
246 × 70:
246
70
-----
17220
在富爷压迫下的人民,你们准备好了吗?

Solution

只是一道简单的模拟题,但是坑有点多(注意乘出零时,零要传递到下一个答案),而且当时我弄了一个多小时的电脑,感觉时间很紧,有点心急,样例都没对完就换题了。

Code

const wr='Problem ';
var
    a,b,tot,t,num:int64;
    sum,m,i,j,n,len,cas:longint;
    ans:array[1..50] of string;
    st:string;
function max(x,y:longint):longint;
begin
    if x>y then exit(x);exit(y);
end;
begin
    readln(a,b);
    while (a<>0)or(b<>0) do
    begin
        fillchar(ans,sizeof(ans),0);
        str(a,ans[1]);str(b,ans[2]);
        sum:=0;len:=max(length(ans[1]),length(ans[2]));n:=2;inc(cas);
        num:=a*b;
        while b>0 do
        begin
            t:=b mod 10;
            b:=b div 10;
            tot:=a*t;
            inc(sum);
            if tot=0 then
            begin
                ans[n+1]:=ans[n+1]+'0';
                inc(m);
                continue;
            end;
            inc(n);
            st:=ans[n];
            str(tot,ans[n]);
            ans[n]:=ans[n]+st;
            for i:=1 to sum-1-m do ans[n]:=ans[n]+' ';
            m:=0;
            len:=max(len,length(ans[n]));
        end;
        writeln(wr,cas);
        inc(n);str(num,ans[n]);len:=max(len,length(ans[n]));
        for i:=1 to len-length(ans[1]) do write(' ');writeln(ans[1]);
        for i:=1 to len-length(ans[2]) do write(' ');writeln(ans[2]);
        for i:=1 to len do write('-');writeln;
        if n-4>0 then
        begin
            for j:=3 to n-1 do
            begin
                for i:=1 to len-length(ans[j]) do write(' ');writeln(ans[j]);
            end;
            for i:=1 to len do write('-');writeln;
        end;
        for i:=1 to len-length(ans[n]) do write(' ');writeln(ans[n]);
        readln(a,b);
    end;
end.

你可能感兴趣的:(NOIP)