Description

Educators are always coming up with new ways to teach math to students. In 2011, an educational software company, All Computer Math (ACM), developed an application to display products in a traditional grade school math format. ACM is now working on an
updated version of the software that will display results in a lattice format that some students find to be easier when multiplying larger numbers.

An example would be when multiplying 345 * 56 = 19320 as given below, using a lattice grid with 2 rows and 3 columns, which appears inside a surrounding frame:

+---------------+

|   3   4   5   |

| +---+---+---+ |

| |1 /|2 /|2 /| |

| | / | / | / |5|

|1|/ 5|/ 0|/ 5| |

| +---+---+---+ |

|/|1 /|2 /|3 /| |

| | / | / | / |6|

|9|/ 8|/ 4|/ 0| |

| +---+---+---+ |

|/ 3 / 2 / 0    |

+---------------+
The first operand, 345, is displayed above the top of the grid with each digit centered horizontally above its column of the grid, and the second operand, 56, is displayed along the righthand side with each digit
centered vertically at the center of its row in the grid. A single cell of the grid, such as
+---+

|3 /|

| / |

|/ 0|

+---+
represents the product of the digit of the first operand that is above its column and the digit of the second operand that is to the right of its row. In our example, this cell represents the product 5 times 6 = 30 that results when multiplying the 5 in
345 and the 6 in 56. Note that the 10's digit of that product is placed in the upper left portion of this cell and the 1's digit in the lower right.

The overall product is then computed by summing along the diagonals in the lattice that represent the same place values in the result. For example, in our first problem the product 19320 was computed as:

1's digit = 0
10's digit = 5 + 3 + 4 = 12, thus 2 with a carry of 1
100's digit = (1 carry) + 2 + 0 + 2 + 8 = 13, thus 3 with a carry of 1
1000's digit = (1 carry) + 2 + 5 + 1 = 9
10000's digit = 1
The resulting product is placed with the one's digit below the grid at the far right and, depending on its length, with the most significant digits wrapped around the left side of the grid. Each digit of the final product appears perfectly aligned with
the corresponding diagonal summands.

To provide an aesthetic view, we use a series of minus (-) characters for horizontal lines, pipe (|) characters for vertical lines, and slash (/) characters for diagonal lines. Furthermore, we use a plus (+) character wherever a horizontal and vertical line
meet. Each multiplication lattice is subsequently "boxed" by an outer border. There is a row containing the first operand which is between the topmost border and the top line of the grid, and a row between the bottom of the grid and the bottom border, which
contains some portion of the resulting product. There is one column between the leading | and the left edge of the inner grid, which may contain a portion of the resulting product, and one column after the right edge of the inner grid but before the rightmost
| border, which contains the second operand. If the product is not long enough to wrap around the bottom-left corner, the column between the left border and the left edge of the grid will containing only spaces. (See the later example of 3 x 3.)

Leading zeros should be displayed within lattice grid cells, but leading zeros should never be displayed in the product, nor should there ever be a slash (/) character prior to the leading digit of the product. For example, consider the product of 12 * 27
= 324 below:

+-----------+

|   1   2   |

| +---+---+ |

| |0 /|0 /| |

| | / | / |2|

| |/ 2|/ 4| |

| +---+---+ |

| |0 /|1 /| |

| | / | / |7|

|3|/ 7|/ 4| |

| +---+---+ |

|/ 2 / 4    |

+-----------+
Note that in the top-right grid of the lattice, the product 2 * 2 = 04 is displayed with the zero for the tens digit. However, there is no thousands digit displayed in the product 324, nor is there any slash displayed above the digit 3 in that product.

Input

The input contains one or more tests. Each test contains two positive integers, A and B, such that 1 ≤ A ≤ 9999 and 1 ≤ B ≤ 9999. The last data set will be followed by a line containing 0 0.

Output

For each data set, produce the grid that illustrates how to multiply the two numbers using the lattice multiplication technique.

Sample Input

345 56
12 27
1 68
9999 7
3 3
0 0

Sample Output

+---------------+
| 3 4 5 |
| +---+---+---+ |
| |1 /|2 /|2 /| |
| | / | / | / |5|
|1|/ 5|/ 0|/ 5| |
| +---+---+---+ |
|/|1 /|2 /|3 /| |
| | / | / | / |6|
|9|/ 8|/ 4|/ 0| |
| +---+---+---+ |
|/ 3 / 2 / 0 |
+---------------+
+-----------+
| 1 2 |
| +---+---+ |
| |0 /|0 /| |
| | / | / |2|
| |/ 2|/ 4| |
| +---+---+ |
| |0 /|1 /| |
| | / | / |7|
|3|/ 7|/ 4| |
| +---+---+ |
|/ 2 / 4 |
+-----------+
+-------+
| 1 |
| +---+ |
| |0 /| |
| | / |6|
| |/ 6| |
| +---+ |
| |0 /| |
| | / |8|
|6|/ 8| |
| +---+ |
|/ 8 |
+-------+
+-------------------+
| 9 9 9 9 |
| +---+---+---+---+ |
| |6 /|6 /|6 /|6 /| |
| | / | / | / | / |7|
|6|/ 3|/ 3|/ 3|/ 3| |
| +---+---+---+---+ |
|/ 9 / 9 / 9 / 3 |
+-------------------+
+-------+
| 3 |
| +---+ |
| |0 /| |
| | / |3|
| |/ 9| |
| +---+ |
| 9 |
+-------+

题意即为模拟乘法。是模拟题,其中旁边的数字是直接用m*n然后依次取各个位上的数。

#include<stdio.h>
#include<string.h>
int a[10],b[10],c[10],d[10],e[10],f;
int main()
{
    int n,m,i,j,k,p,flag,sum,t,s,num,num1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0 && m==0)
        break;
        num=n*m;
        t=0;
        while(num>0)
        {
            t++;
            e[t]=num%10;
            num=num/10;
            num1=t;
        }
         
         
        t=0;
        while(n>0)
        {
            t++;
            a[t]=n%10;
            n=n/10;
            p=t;
        }
        for(i=1;i<=p;i++)
        {
            c[i]=a[p+1-i];
        }
        t=0;
        while(m>0)
        {
            t++;
            b[t]=m%10;
            m=m/10;
            k=t;
        }
        for(i=1;i<=k;i++)
        {
            d[i]=b[k+1-i];
        }
        printf("+");
        for(i=1;i<=p*3+p+1+2;i++)
        printf("-");
        printf("+\n");
        for(i=1;i<=4*p+5;i++)
        {
            if(i==1 || i==4*p+5)
            printf("|");
            else if((i-1)%4==0)
            {
              printf("%d",c[(i-1)/4]);           
            }
            else printf(" ");
        }
        printf("\n");
        flag=0;
        for(i=1;i<=k;i++)
        {
            printf("| ");
            for(j=1;j<=p;j++)
            printf("+---");
            printf("+ |\n");
             
            if(flag==0)
            printf("| |");
            else printf("|/|");
            for(j=1;j<=p;j++)
            {
                printf("%d /|",c[j]*d[i]/10);   
            }
            printf(" |\n");
             
            printf("| |");
            for(j=1;j<=p;j++)              
            {
                printf(" / |");
            }
            printf("%d|\n",d[i]);
             
            printf("|");
            if(num1==p+k-i+1)
            {
                flag=1;
                printf("%d",e[num1]);
                num1--;
            }
            else printf(" ");
            printf("|");
            for(j=1;j<=p;j++)
            {
                printf("/ %d|",d[i]*c[j]%10);
            }
            printf(" |\n");
        }
         
            printf("| ");
            for(j=1;j<=p;j++)
            printf("+---");
            printf("+ |\n"); 
             
            printf("|");
            for(i=1;i<=p;i++)
            {
                if(i==1)
                {
                    if(flag==1)
                    printf("/ ");
                    else printf("  ");
                    printf("%d ",e[num1]);
                    num1--;
                    continue;
                }
                printf("/ %d ",e[num1]);
                num1--;
            }
            printf("   |\n");
             
            printf("+");
            for(i=1;i<=p*3+p+1+2;i++)
            printf("-");
            printf("+\n");
    }
    return 0;
}

1561: (More) Multiplication的更多相关文章

  1. POJ2505 A multiplication game[博弈论]

    A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6028   Accepted:  ...

  2. 【数学】Matrix Multiplication

                                 Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  3. hdu 4920 Matrix multiplication bitset优化常数

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  4. 矩阵乘法 --- hdu 4920 : Matrix multiplication

    Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/ ...

  5. Booth Multiplication Algorithm [ASM-MIPS]

    A typical implementation Booth's algorithm can be implemented by repeatedly adding (with ordinary un ...

  6. hdu4951 Multiplication table (乘法表的奥秘)

    http://acm.hdu.edu.cn/showproblem.php?pid=4951 2014多校 第八题 1008 2014 Multi-University Training Contes ...

  7. hdu4920 Matrix multiplication 模3矩阵乘法

    hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 ...

  8. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  9. 矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence

    题目传送门 /* 题意:加上适当的括号,改变计算顺序使得总的计算次数最少 矩阵连乘积问题,DP解决:状态转移方程: dp[i][j] = min (dp[i][k] + dp[k+1][j] + p[ ...

随机推荐

  1. 【MySQL 基础】MySQL必知必会

    MySQL必知必会 简介 <MySQL必知必会>的学习笔记和总结. 书籍链接 了解SQL 数据库基础 什么是数据库 数据库(database):保存有组织的数据的容器(通常是一个文 件或一 ...

  2. Openstack Ocata 负载均衡安装(二)

    Openstack OCATA 负载节点(二) 安装haproxy: apt install haproxy 配置haproxy: vim /etc/haproxy/haproxy.cfg globa ...

  3. redis之集群二:哨兵

    回顾 上一篇介绍了Redis的主从集群模式,这个集群模式配置很简单,只需要在Slave的节点上进行配置,Master主节点的配置不需要做任何更改.但是,我们发现这种集群模式当主节点宕机,主从无法自动切 ...

  4. LeetCode671. 二叉树中第二小的节点

    题目 纯暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 int findSecondMinimumValue(TreeNode* r ...

  5. 【葵花宝典】All-in-One模式安装KubeSphere

    1.准备 Linux 机器 2.google api受限下载 KubeKey export KKZONE=cn curl -sfL https://get-kk.kubesphere.io | VER ...

  6. MySQL下载与安装教程

    一,下载篇 1,首先访问MySQL官网下载页,https://dev.mysql.com/downloads/mysql/ 如果是MAC系统,操作系统请选择macOS,Windows则选择Window ...

  7. [USACO2011 Feb] Cow Line

    原题链接https://www.lydsy.com/JudgeOnline/problem.php?id=3301 康拓展开和逆展开的模板题. #include<iostream> #in ...

  8. 配置Charles 设置手机代理并允许https请求

    前言: 在h5开发调试时,为实现手机app访问localhost地址,可以使用ip地址的方式,但一般公司app出于安全考虑,会限制只能访问其自有域名.因此,使用charles代理的方式 步骤 用手机代 ...

  9. 使用bandit对目标python代码进行安全函数扫描

    技术背景 在一些对python开源库代码的安全扫描中,我们有可能需要分析库中所使用到的函数是否会对代码的执行环境造成一些非预期的影响.典型的例如python的沙箱逃逸问题,通过一些python的第三方 ...

  10. (Oracle)索引实战(转载)

    人们在使用SQL时往往会陷入一个误区,即太关注于所得的结果是否正确,而忽略了不同的实现方法之间可能存在的性能差异,这种性能差异在大型的或是复杂的数据库环境中(如联机事务处理OLTP或决策支持系统DSS ...