hdu4207 Grade School Multiplication

Grade School Multiplication

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 160    Accepted Submission(s): 59


Problem Description
An educational software company, All Computer Math (ACM), has a section on multiplication of integers. They want to display the calculations in the traditional grade school format, like the following computation of 432 × 5678:

    432
   5678
-------
   3456
  3024
 2592
2160
-------
2452896

Note well that the final product is printed without any leading spaces, but that leading spaces are necessary on some of the other lines to maintain proper alignment. However, as per our regional rules, there should never be any lines with trailing white space. Note that the lines of dashes have length matching the final product.

As a special case, when one of the digits of the second operand is a zero, it generates a single 0 in the partial answers, and the next partial result should be on the same line rather than the next line down. For example, consider the following product of 200001 × 90040:

     200001
      90040
-----------
    8000040
180000900
-----------
18008090040

The rightmost digit of the second operand is a 0, causing a 0 to be placed in the rightmost column of the first partial product. However, rather than continue to a new line, the partial product of 4 × 200001 is placed on the same line as that 0. The third and fourth least-significant digits of the second operand are zeros, each resulting in a 0 in the second partial product on the same line as the result of 9 × 200001.

As a final special case, if there is only one line in the partial answer, it constitutes a full answer, and so there is no need for computing a sum. For example, a computation of 246 × 70 would be formatted as
  246
   70
-----
17220

Your job is to generate the solution displays.
 

Input
The input contains one or more data sets. Each data set consists of two positive integers on a line, designating the operands in the desired order. Neither number will have more than 6 digits, and neither will have leading zeros. After the last data set is a line containing only 0 0.
 

Output
For each data set, output a label line containing "Problem " with the number of the problem, followed by the complete multiplication problem in accordance with the format rules described above.
 

Sample Input
  
  
432 5678 200001 90040 246 70 0 0
 

Sample Output
  
  
Problem 1 432 5678 ------- 3456 3024 2592 2160 ------- 2452896 Problem 2 200001 90040 ----------- 8000040 180000900 ----------- 18008090040 Problem 3 246 70 ----- 17220
 

Source
 

Recommend
lcy




简单模拟,不解释,直接上代码,注意第二个数中0的处理。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int Count(long long t)
{
    int ret=0;
    while(t!=0)
    {
        t/=10;
        ret++;
    }
    return ret;
}

void Print(int t,char ch)
{
    int i;
    for (i=0;i<t;i++)
    {
        printf("%c",ch);
    }
}


int main()
{
    int i,j,t,len,s,cnt=1;
    long long n,m,ans,tag,k;
    while(1)
    {
        scanf("%I64d%I64d",&n,&m);
        if (n==0 && m==0) break;
        printf("Problem %d\n",cnt++);
        ans=n*m;
        len=Count(ans);
        Print(len-Count(n),' ');
        printf("%I64d\n",n);
        Print(len-Count(m),' ');
        printf("%I64d\n",m);
        Print(len,'-');
        printf("\n");
        t=0;
        tag=m;
        while(tag)
        {
            if (tag%10!=0) break;
            t++;
            tag/=10;
        }
        if (Count(m)-t==1)
        {
            printf("%I64d\n",ans);
            continue;
        }
        s=0;
        t=0;
        while(m)
        {
            if (m%10==0)
            {
                m/=10;
                s++;
                t++;
                continue;
            }
            k=m%10*n;
            Print(len-s-Count(k),' ');
            printf("%I64d",k);
            if (t!=0)
            {
                Print(t,'0');
                t=0;
            }
            printf("\n");
            s++;
            m/=10;
        }
        Print(len,'-');
        printf("\n%I64d\n",ans);
    }
    return 0;
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值