CSU 1561 (More) Multiplication

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<cstdio>  
#include<cstring>
#include<cstdio>
#include<algorithm>  
using namespace std;
const int N = 1e3 + 10;
int n, m, o, A, B, C;
int a[N], b[N], c[N];

void putout() {
	printf("+");
	for (int i = 1; i <= A * 4 + 3; i++) printf("-");
	printf("+\n");

	printf("|");
	for (int i = 1; i <= A * 4 + 3; i++) {
		if (i % 4 == 0) printf("%d", a[i / 4]); else printf(" ");
	}
	printf("|\n");

	for (int j = 1; j <= B; j++) {
		
		printf("| ");
		for (int i = 1; i <= A; i++) printf("+---");
		printf("+ |\n");

		if (A + B == C) printf("|%c", j == 1 ? ' ': '/');
		else printf("|%c", j <= 2 ? ' ' : '/');
		for (int i = 1; i <= A; i++) {
			printf("|%d /", b[j] * a[i] / 10);
		}
		printf("| |\n");

		printf("| ");
		for (int i = 1; i <= A; i++) {
			printf("| / ");
		}
		printf("|%d|\n", b[j]);

		if (A + B == C) printf("|%d", c[j]);
		else if (j == 1) printf("| "); else printf("|%d", c[j - 1]);
		for (int i = 1; i <= A; i++) {
			printf("|/ %d", b[j] * a[i] % 10);
		}
		printf("| |\n");
	}

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

	printf("|%c", A == C ? ' ' : '/');
	for (int i = 1; i <= A; i++) printf(" %d %c", c[C - A + i], i == A ? ' ' : '/');
	printf("  |\n");

	printf("+");
	for (int i = 1; i <= A * 4 + 3; i++) printf("-");
	printf("+\n");
}
int main(){
	while (~scanf("%d%d", &n, &m) && n + m) {
		A = B = C = 0; o = n * m;
		for (; n; n /= 10) a[++A] = n % 10;
		reverse(a + 1, a + A + 1);
		for (; m; m /= 10) b[++B] = m % 10;
		reverse(b + 1, b + B + 1);
		for (; o; o /= 10) c[++C] = o % 10;
		reverse(c + 1, c + C + 1);
		putout();
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值