(More) Multiplication
Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 242 Solved: 129Description
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 |
+---------------+
|3 /|
| / |
|/ 0|
+---+
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
|
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 |
+-----------+
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.
Source
题意:给你两个数,按题目的输出方法模拟出它们的乘法过程
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n,m;
int a[10],b[10],ans[10][10];
int main()
{
while(~scanf("%d%d",&n,&m)&&(n+m))
{
int cnt1=0,cnt2=0,cnt3=0,ans1=n*m,res=0,flag=0;
while(n) {a[cnt1++]=n%10;n/=10;}
while(m) {b[cnt2++]=m%10,m/=10;}
for(int i=0;i<cnt1/2;i++) swap(a[i],a[cnt1-i-1]);
for(int i=0;i<cnt2/2;i++) swap(b[i],b[cnt2-i-1]);
for(int i=0;i<cnt2;i++)
for(int j=0;j<cnt1;j++) ans[i][j]=a[j]*b[i];
while(ans1) {ans[cnt2][cnt3++]=ans1%10,ans1/=10;}
for(int i=0;i<cnt3/2;i++) swap(ans[cnt2][i],ans[cnt2][cnt3-i-1]);
printf("+");
for(int i=0;i<cnt1*4+3;i++) printf("-");
printf("+\n");
printf("|");
for(int i=0;i<cnt1;i++) printf(" %d",a[i]);
printf(" |\n");
printf("| +");
for(int i=0;i<cnt1;i++) printf("---+");
printf(" |\n");
for(int i=0;i<cnt2;i++)
{
printf("|");
if(flag) printf("/");
else printf(" ");
printf("|");
for(int j=0;j<cnt1;j++) printf("%d /|",ans[i][j]/10);
printf(" |\n");
printf("| |");
for(int j=0;j<cnt1;j++) printf(" / |");
printf("%d|\n",b[i]);
printf("|");
if(i==cnt1+cnt2-cnt3||flag) printf("%d|",ans[cnt2][res++]),flag=1;
else printf(" |");
for(int j=0;j<cnt1;j++) printf("/ %d|",ans[i][j]%10);
printf(" |\n");
printf("| +");
for(int i=0;i<cnt1;i++) printf("---+");
printf(" |\n");
}
printf("|");
if(flag) printf("/");
else printf(" ");
for(int i=0;i<cnt1;i++)
{
printf(" %d ",ans[cnt2][res++]);
if(i!=cnt1-1) printf("/");
}
printf(" |\n");
printf("+");
for(int i=0;i<cnt1*4+3;i++) printf("-");
printf("+\n");
}
return 0;
}