(more) Multiplication Time limit:1000ms Memory limit:65536k Title Description
Educators is always coming up with the new ways to teach math to students. In-educational software company, all computer Math (ACM), developed an application-display products in a Tradi tional Grade School math format. ACM is now working on an updated version of the software that would display results in a lattice format that some students Find to is easier when multiplying larger numbers.
An example would is when multiplying 345 * = 19320 as given below, using a lattice grid with 2 rows and 3 columns, whic H appears inside a surrounding frame:
+---------------+| 3 4 5 | | +---+---+---+ | | | |/|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 T He grid, and the second operand, and the is displayed along the righthand side with each digit centered vertically at the cent Er 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's above its column and the digit of the second operand That's the right of the IT row. In we example, this cell represents the product 5 times 6 = That results when multiplying the 5 in 345 and the 6 in 56. Note that the ten ' s digit of that product are placed in the upper left portion of this cell and the 1 's digit in the lower R Ight.
The overall product is then computed by summing along, the diagonals in the lattice, the represent place value s in the result. For example, with our first problem the product 19320 is computed as:
1 ' s digit |
= 0 |
Ten ' s digit |
= 5 + 3 + 4 = Thus 2 with a carry of 1 |
+ ' s digit |
= (1 carry) + 2 + 0 + 2 + 8 =, thus 3 with a carry of 1 |
Digit ' s |
= (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 th e 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 are subsequently "boxed" by an outer border. There is a row containing the first operand which are between the topmost border and the top line of the grid, and a row being Tween 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 could 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 a long enough to wrap around the bottom-left corner, the column between the left border and the left E Dge of the grid would containing only spaces. (see the later example of 3 x 3 .)
Leading zeros should is displayed within lattice grid cells, but leading zeros should never is displayed in the product, n Or should there ever is a slash ( / ) character prior to the leading digit of the product. For example, consider the product of * = 324 below:
+-----------+| 1 2 | | +---+---+ | | | |/|0/| | | |/|/|2| | |/2|/4| | | +---+---+ | | | | |//|1 | 3|/7|/4| | | +---+---+ || /2/4 |+-----------+
Note that in the top-right grid of the lattice, the product 2 * 2 = are displayed with the zero for the Tens digit. However, there is no thousands digit displayed in the product 324, nor was there any slash displayed above the digit 3 in t Hat product.
InputThe input contains one or more tests. Each test contains the positive integers, A and B, such that 1≤a≤9999 and 1≤b≤9999 . The last data set would be followed to a line containing 0 0 .
Outputfor each data set, produce the grid, which illustrates how to multiply the numbers using the lattice multiplication Technique.
Sample input
345 5612 271 689999 73 30 0
Sample output
+---------------+| 3 4 5 | | +---+---+---+ | | | |/|2/|2/| | | |/|/|/|5| | 1|/5|/0|/5| | | +---+---+---+ || /|1/|2/|3/| | | | / | / | /|6| | 9|/8|/4|/0| | | +---+---+---+ || /3/2/0 |+---------------+ +-----------+| 1 2 | | +---+---+ | | | |/|0/| | | |/|/|2| | |/2|/4| | | +---+---+ | | | | |//|1 | 3|/7|/4| | | +---+---+ || /2/4 |+-----------+ +-------+| 1 | | +---+ | | |/| | | |/|6| | |/6| | | +---+ | | | | | | |//| | 6|/8| | | +---+ || /8 |+-------+ +-------------------+| 9 9 9 9 | | +---+---+---+---+ | | |6/|6/|6/|6/| | | |/|/|/|/|7| | 6|/3|/3|/3|/3| | | +---+---+---+---+ || /9/9/9/3 |+-------------------+ +-------+| 3 | | +---+ | | | |/| | | |/|3| | |/9| | | +---+ | | 9 |+-------+
TipsThe tables must is formatted precisely as outlined by the rules and examples provided. Mistakes that involve solely errant whitespace would be categorized as Presentation Error ; all other errors'll be reported as wrong Answer .SOURCE ACM mid-central reginal programming Contest (MCPC2014) sample program
#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 (str 2, "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 * + (Str1[i]-' 0 '); KK = KK *10; } for (int i=0; i<m; i++) {P2 = P2 * + (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) {Prin TF ("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;}
Sdut 3183 (more) multiplication (analog)