SDUT 3183 (More) Multiplication(模拟)

(More) Multiplication

Time Limit: 1000MS Memory limit: 65536K

题目描述

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 aslash (/) 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.

输入

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.

输出

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

示例输入

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

示例输出

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

提示

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

来源

2014 ACM MId-Central Reginal Programming Contest(MCPC2014)

示例程序

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int n,m;
char str1[101],str2[101];
long long int psum;

struct node
{
    int x;
    int y;
} mm[101][101];

int main()
{
    while(scanf("%s%s",str1,str2)!=EOF)
    {
        if(strcmp(str1,"0") == 0 && strcmp(str2,"0") == 0)
        {
            break;
        }
        int flag = 0;
        long long int p1 = 0,p2 = 0,kk = 1;
        n = strlen(str1);
        m = strlen(str2);
        for(int i=0; i<n; i++)
        {
            p1 = p1 * 10 + (str1[i] - '0');
            kk = kk *10;
        }
        for(int i=0; i<m; i++)
        {
            p2 = p2 * 10 + (str2[i] - '0');
            kk = kk * 10;
        }
        for(int j=0; j<n; j++)
        {
            for(int k=0; k<m; k++)
            {
                mm[j][k].x = 0;
                mm[j][k].y = 0;
            }
        }
        kk = kk / 10;
        psum = p1*p2;
        for(int j=0; j<m; j++)
        {
            for(int k=0; k<n; k++)
            {
                int sum = (str1[k]-'0')*(str2[j]-'0');
                mm[j][k].x = sum/10;
                mm[j][k].y = sum%10;
            }
        }
        int pn = (n+1)*3+n;
        printf("+");
        for(int i=0; i<pn; i++)
        {
            printf("-");
        }
        printf("+\n");
        printf("|");
        for(int i=0; i<n; i++)
        {
            printf("   %c",str1[i]);
        }
        printf("   |\n");

        for(int v=0; v<m; v++)
        {
            printf("| +");
            for(int i=0; i<n; i++)
            {
                printf("---+");
            }
            printf(" |\n");
            if(v == 0 || flag == 0)
            {
                printf("| |");
            }
            else
            {
                printf("|/|");
            }
            for(int j=0; j<n; j++)
            {
                printf("%d /|",mm[v][j].x);
            }
            printf(" |\n");
            printf("| |");
            for(int j=0; j<n; j++)
            {
                printf(" / |");
            }
            printf("%c|\n",str2[v]);
            printf("|");
            if(psum/kk!=0)
            {
                printf("%d",(psum/kk)%10);
                flag = 1;
            }
            else if((psum/kk)%10 == 0)
            {
                if(flag == 1)
                {
                    printf("0");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("|");
            for(int j=0; j<n; j++)
            {
                printf("/ %d|",mm[v][j].y);
            }
            printf(" |\n");
            kk = kk/10;
        }
        printf("| +");
        for(int i=0; i<n; i++)
        {
            printf("---+");
        }
        printf(" |\n");
        printf("|");
        for(int j=0; j<n; j++)
        {
            if(j == 0 && flag == 0)
            {
                printf("  %d ",(psum/kk)%10);
            }
            else
            {
                printf("/ %d ",(psum/kk)%10);
            }

            kk = kk/10;
        }
        printf("   |\n");
        printf("+");
        for(int i=0; i<pn; i++)
        {
            printf("-");
        }
        printf("+\n");

    }
    return 0;
}
时间: 2024-10-09 22:06:31

SDUT 3183 (More) Multiplication(模拟)的相关文章

POJ 3183:Stump Removal(模拟)

http://poj.org/problem?id=3183 题意:有n个树桩,分别有一个高度h[i],要用Bomb把树桩都炸掉,如果炸的位置的两边树桩高度小于Bomb炸的树桩高度,那么小于树桩高度的两侧都是可以被炸掉的.而且有传递性.求把树桩全部炸掉要消耗的最少的Bomb数所炸的位置. 看样例: 1 2 5 4 3 3 6 6 2 炸位置3(即5)的话,那么2小于5,所以位置2被炸掉,因为1小于2,所以位置1被炸掉,因为4小于5,所以位于4被炸掉,因为3小于4,所以位置5被炸掉.因此丢一个Bo

SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )

图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天灾,一个叫近卫.在他们所在的地域,有n个隘口,编号为1..n,某些隘口之间是有通道连接的.其中近卫军团在1号隘口,天灾军团在n号隘口.某一天,天灾军团的领袖巫妖王决定派兵攻打近卫军团,天灾军团的部队如此庞大,甚至可以填江过河.但是巫妖王不想付出不必要的代价,他想知道在不修建任何通道的前提下,部队是否

SDUT 2162-The Android University ACM Team Selection Contest(模拟)

The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Now it's 20000 A.D., and the androids also participate in the ACM Inter-national Collegiate Programming Contest (ACM/ICPC). In order to selec

UVa 442 Matrix Chain Multiplication(矩阵链乘,模拟栈)

题意  计算给定矩阵链乘表达式需要计算的次数  当前一个矩阵的列数等于后一个矩阵的行数时  他们才可以相乘  不合法输出error 输入是严格合法的  即使只有两个相乘也会用括号括起来  而且括号里最多有两个 那么就很简单了 遇到字母直接入栈  遇到反括号计算后入栈  然后就得到结果了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int N = 1000;

SDUT 2411 Pixel density(模拟,字符串)

题目链接:SDUT 2411 Pixel density 这一题,可以说题目略坑.为什么呢,第一就是他题意描述过于模糊,第二题输出格式有点坑(说到底还是题意不要表述清晰).现在我们来看看这一题吧,这一题是 <2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛> 的题.本题可以说是一道着重于细节的一道题.对于细节没有把握清楚的话,就是WA.据说当时某些队伍就是因为细节没有把握好,结果硬是WA了26次(深表同情啊).我们仔细观察题目的话,可以发现 他给我们的格式是 &qu

UVALive 2323 Modular Multiplication of Polynomials(模拟)

这是一个相对简单的模拟,因为运算规则已经告诉了我们,并且比较简单,不要被吓到…… 思路:多项式除以另外一个多项式,如果能除,那么他的最高次一定被降低了,如果最高次不能被降低,那说明已经无法被除,就是题目要求输出的膜了,降低最高次的方法很简单,只要被除式的最高次 >= 除式的最高次,就将除式的最高次升高到与被除式一样高,然后让被除式减去它,直到不满足上述关系为止. 代码如下: #include<cstdio> #include<algorithm> #include<io

SDUT 3165 Round Robina(模拟)

Round Robina Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Suppose that N players sit in order and take turns in a game, with the first person following the last person, to continue in cyclic order. While doing so, each player keeps trac

UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)

题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的次数,累加就可以了. PS:注意弹出的矩阵表示的先后顺序. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1000000000 #define mod 1000000007 #define FRE() freopen

ACDream 1213 Matrix Multiplication (01矩阵处理)

Matrix Multiplication Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Let us consider undirected graph G = {V; E} which has N vertices and M edges. Incidence matrix of