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

解题:

此题不仅看上去烦,其实是真烦。不过,要输出的值都可以先算出来,保存下来,输出的时候加以判断就好了。像结果保存在string和int中。中间过程保留在二维数组中。比较坑的一点是左边和左下角的”/“,如果之前没有值的话,是不需要的,这里写起来挺烦的。能够1A还是挺高兴的!

代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
int store[5][5];
int toInt(string s)
{
	int ans=0,t=1;
	reverse(s.begin(),s.end());
	for(int i=0;i<s.length();i++)
	{
		ans+=(s[i]-'0')*t;
		t*=10;
	}
	return ans;
}
string toString(int x)
{
	string res="";
	if(x==0)
	{
		res+="0";
		return res;
	}
	while(x)
	{
		res+=char(x%10+'0');
		x/=10;
	}
	reverse(res.begin(),res.end());
	return res;
}
int main()
{
	string a,b,ans;
	int ia,ib,ians,cnt;
	bool flag;
	while(cin>>a>>b)
	{
		cnt=0;
		flag=false;
	   ia=toInt(a);
	   ib=toInt(b);
	   if(!(ia||ib))break;
	   ians=ia*ib;
	   ans=toString(ians);
	   for(int i=0;i<b.length();i++)
	   {
   		 for(int j=0;j<a.length();j++)
   		 store[i][j]=(b[i]-'0')*(a[j]-'0');
   	   }
   	   //1
   	   cout<<"+"<<string((a.length()-1)*4+7,'-')<<"+\n";
   	   //2
   	   cout<<'|'<<string(3,' ');
   	   for(int i=0;i<a.length();i++)
   	   {
   	   	 cout<<a[i]<<string(3,' ');
   	   }
   	   cout<<"|\n";
   	   //3
   	   cout<<"| +";
   	   for(int i=0;i<a.length();i++)
   	   {
   	   	 cout<<"---+";
   	   }
   	   cout<<" |"<<"\n";
   	   for(int i=0;i<b.length();i++)
   	   {
   	   	//4+T*n
   	   	 cout<<"|";
   	   	 if(!flag)
	     {
     	   if(ans.length()+i>=(a.length()+b.length()))
     	   {
			 flag=true;
     	   }
			cout<<' ';
	     }
   	   	 else cout<<'/';
	     for(int j=0;j<a.length();j++)
		 {
 			cout<<'|';
 			cout<<char(store[i][j]/10+'0')<<' '<<'/';
 		 }
 		 cout<<"| |\n";
 		 //5+T*n
 		 cout<<"| ";
 		 for(int j=0;j<a.length();j++)
 		   cout<<"| / ";
 		 cout<<"|"<<b[i]<<"|\n";
 		 //6+T*n
 		 cout<<"|";
 		 if((i+ans.length())>=(a.length()+b.length()))
 		 cout<<ans[cnt++];
 		 else cout<<" ";
 		 for(int j=0;j<a.length();j++)
 		 {
 		 	cout<<"|/ ";
 		 	cout<<char(store[i][j]%10+'0');
 		 }
 		 cout<<"| |\n";
 		 //7+T*n
 		 cout<<"| +";
 		 for(int j=0;j<a.length();j++)
 		 cout<<"---+";
 		 cout<<" |\n";
   	   }
   	   //倒数第2行
	   cout<<"|";
	   if(ans.length()>a.length())
	   cout<<"/";
	   else cout<<" ";
	   for(int i=0;i<a.length()-1;i++)
	    {
		  cout<<" ";
		  cout<<ans[ans.length()-a.length()+i];
		  cout<<" /";
	    }
	    cout<<" "<<ans[ans.length()-1]<<string(4,' ')<<"|\n";
	    //最后一行
		cout<<"+"<<string((a.length()-1)*4+7,'-')<<"+\n";
	}
	return 0;
}

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

时间: 2024-12-14 18:48:41

CSU 1561-(More) Multiplication的相关文章

湖南多校对抗赛(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

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 C

CSU 1804: 有向无环图(拓扑排序)

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1804 题意:…… 思路:对于某条路径,在遍历到某个点的时候,之前遍历过的点都可以到达它,因此在这个时候对答案的贡献就是∑(a1 + a2 + a3 + ... + ai) * bv,其中a是之前遍历到的点,v是当前遍历的点. 这样想之后就很简单了.类似于前缀和,每次遍历到一个v点,就把a[u]加给a[v],然后像平时的拓扑排序做就行了. 1 #include <bits/stdc++.h>

CSU 1111: 三家人【有趣的思维题】

1111: 三家人 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 2241  Solved: 874 [Submit][Status][Web Board] Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列,便出了90元.请问这笔钱如何分给A.B 二位太太较为恰当?A 应得多少元?90/(5+4)*5=$50

CSU 1112: 机器人的指令【模拟题】

1112: 机器人的指令 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1858  Solved: 682 [Submit][Status][Web Board] Description 数轴原点有一个机器人.该机器人将执行一系列指令,你的任务是预测所有指令执行完毕之后它的位置. ·LEFT:往左移动一个单位 ·RIGHT: 往右移动一个单位 ·SAME AS i: 和第i 条执行相同的动作.输入保证i 是一个正整数,且不超过之前执行指令数 In

hdu 1561 The more, The Better 树状DP

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5844    Accepted Submission(s): 3476 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

[HDU 1561] The more, The Better (树形dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 题目大意:ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡.你能帮ACboy算出要获得尽量多的宝物应该攻克哪M个城堡吗? 设计状态 dp[u][i] 代表以u为根的,选择i个的最大收获 状态转移 dp[u][j+k] =

CSU 1416 Practical Number

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1416 结论题,具体判断方法请点击这个网址. 筛素数是肯定的,但一开始定的范围太大了,想当然要筛到10^9的质数,但仔细想想,只要到sqrt(10^9)就可以了,最后的那一个质数是最后一步的比较,不用筛出来. #include <stdio.h> #include <string.h> #include <iostream> using namespace st

CSU 1412 Line and Circles

原题链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1412 题目要求判断是否有一条直线可以穿过所有的圆. 做法:把所有圆心做一次凸包,然后判断这个凸包是否能通过一个宽度为2*R的通道. 做法和求凸包直径差不多,只是判断的时候把点到两个端点的距离换成点到直线的距离. #include <stdio.h> #include <string.h> #include <math.h> #include <stdli