1561: (More) Multiplication(模似)

1561: (More) Multiplication

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 86  Solved: 48

[Submit][Status][Web
Board
]

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    |
+-------+

HINT

The tables must be formatted precisely as outlined by the rules and examples provided. Mistakes that involve solely errant whitespace will be categorized as Presentation Error; all other errors will be reported as Wrong Answer.

#include<stdio.h>
const int N =100;

int main()
{
    int A,B,C,a[N],b[N],c[N],lenA,lenB,lenC,flag;
    while(scanf("%d%d",&A,&B)>0)
    {
        if(A==0&&B==0)
            break;
        C=A*B;
        lenA=lenB=lenC=0;
        while(A)
        {
            a[++lenA]=A%10; A/=10;
        }
        while(B)
        {
            b[++lenB]=B%10; B/=10;
        }
        while(C)
        {
            c[++lenC]=C%10; C/=10;
        }
        for(int i=1,j=lenA;i<j;i++,j--)
        {
            A=a[i];a[i]=a[j];a[j]=A;
        }
        flag=lenC;
        printf("+-");
        for(int i=1;i<=lenA;i++)
            printf("----");
        printf("--+\n");
        printf("| ");
        for(int i=1;i<=lenA;i++)
            printf("  %d ",a[i]);
        printf("  |\n");

        for(int i=lenB;i>0;i--)
        for(int j=1;j<=4;j++)
        {
            printf("|");
            if(j!=2)
            {
                if(j!=4||lenC-lenA<i)
                printf(" ");
                else printf("%d",c[lenC--]);
            }
            else
            {
                if(flag-lenA>i)printf("/");
                else printf(" ");
            }

            if(j==1)
            {
                printf("+");
                for(int e=1;e<=lenA;e++)
                    printf("---+");
                printf(" |\n");
            }
            else if(j==2)
            {
                for(int e=1;e<=lenA;e++)
                    printf("|%d /",(a[e]*b[i])/10);
                printf("| |\n");
            }
            else if(j==3)
            {
                for(int e=1;e<=lenA;e++)
                    printf("| / ");
                printf("|%d|\n",b[i]);
            }
            else
            {
                for(int e=1;e<=lenA;e++)
                    printf("|/ %d",(a[e]*b[i])%10);
                printf("| |\n");
            }
        }
        printf("| +");
        for(int e=1;e<=lenA;e++)
            printf("---+");
                printf(" |\n");

        printf("|");
        if(flag>lenA)
            printf("/");
        else printf(" ");
        while(lenC)
        {
            printf(" %d ",c[lenC--]);
            if(lenC)  printf("/");
        }
        printf("   |\n");

        printf("+-");
        for(int i=1;i<=lenA;i++)
            printf("----");
        printf("--+\n");

    }
}
时间: 2024-12-11 04:23:10

1561: (More) Multiplication(模似)的相关文章

hdu4920 Matrix multiplication 模3矩阵乘法

hdu4920 Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 568    Accepted Submission(s): 225 Problem Description Given two matrices A and B of size n×n, find the product o

CSU 1561-(More) Multiplication

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1561 题面: 1561: (More) Multiplication Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 180  Solved: 95 [Submit][Status][Web Board] Description Educators are always coming up with new ways to teach m

湖南多校对抗赛(2015.4.6)CSU 1561~1569 题解

A:点击打开链接 CSU 1561 (More)Multiplication 题意:把两个数的乘积每个位置的结果填充上去. 注意这个矩阵最大是1e8,所以不能开数组. 模拟题 #include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <queue

javaweb学习总结(三十八)——事务

一.事务的概念 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 例如:A——B转帐,对应于如下两条sql语句  update from account set money=money+100 where name='B';  update from account set money=money-100 where name='A'; 二.MySQL数据库中操作事务命令 1.编写测试SQL脚本,如下: 1 /*创建账户表*/ 2 create table acco

四种常见的 POST 提交数据方式

HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文主要讨论 POST 提交数据的几种方式. 我们知道,HTTP 协议是以 ASCII 码传输,建立在 TCP/IP 协议之上的应用层规范.规范把 HTTP 请求分为三个部分:状态行.请求头.消息主体.类似于下面这样: BASH<method> <request-URL> <vers

C#中的表达式树的浅解

表达式树可以说是Linq的核心之一,为什么是Linq的核心之一呢?因为表达式树使得c#不再是仅仅能编译成IL,我们可以通过c#生成一个表达式树,将结果作为一个中间格式,在将其转换成目标平台上的本机语言.比如SQL.我们常用的Linq to sql就是这样生成SQL的. 表达式树是.NET 3.5之后引入的,它是一个强大灵活的工具(比如用在LINQ中构造动态查询). 先来看看Expression类的API接口: namespace System.Linq.Expressions { // // 摘

Ubuntu部分快捷键的使用简介

1.切换到字符界面 Ctrl+Alt+F1 切换到tty1字符界面 Ctrl+Alt+F2 切换到tty2字符界面 Ctrl+Alt+F3 切换到tty3字符界面 Ctrl+Alt+F4 切换到tty4字符界面 Ctrl+Alt+F5 切换到tty5字符界面 Ctrl+Alt+F6 切换到tty6字符界面2.切换到图形界面 Ctrl+Alt+F7或Alt+F7 切换到图形界面 3.打开模拟终端 Ctrl+Alt+T 打开的终端模似了字符终端,而不是真正的终端 Ubuntu部分快捷键的使用简介,码

万年历算法的实现(C语言--gcc编译)

/** cal.c * * 现行的格里历是从儒略历演化而来的.儒略历每4年一个润年,润年366天,平年365天.* 如果从公元1年算的话,那么凡是能够被4整除的都是润年.从天文角度看,儒略历这种 * 历法是有误差的,到16世纪误差已经达到了10天.1582年,罗马教皇对儒略历进行了 * 一次校定,该年的10-5到10-14这10天被抹掉,并规定凡不能被400整除的世纪年不再 * 算为润年,校定之后的儒略历即为现行的格里历. * * 但是英国直到1752年才开始使用格里历,此时时间误差已经达到了1

HttpServletResponse对象

Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了.要向客户机输出数据,只需要找response对象就行了. HttpServletResponse对象是服务器的响应.这个对象中封装了向客户端发送数据.发送响应头,发送响应状态码的方法. //用outputStream输出中文数据的问题 /