CSU 1561-(More) Multiplication
来源:互联网 发布:chromium浏览器 linux 编辑:程序博客网 时间:2024/05/16 17:45
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1561
题面:
1561: (More) Multiplication
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 180 Solved: 95
[Submit][Status][Web Board]
Description
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:
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 5612 271 689999 73 30 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 MBSubmit: 180 Solved: 95
[Submit][Status][Web Board]
Description
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:
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 5612 271 689999 73 30 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.
- CSU 1561-(More) Multiplication
- CSU 1561(More) Multiplication
- 1561: (More) Multiplication(模似)
- 1561: (More) Multiplication
- CSU1561 (More) Multiplication
- CSU1561-(More) Multiplication
- SDUT 3183 (More) Multiplication(模拟)
- Csu-1850-Grade School Multiplication [模拟]
- Multiplication
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- WorkPlanning_4
- jquery validation plugin 使用
- ___attribute__ 用法
- 剑指Offer-->斐波那契数列(三种实现方法)
- 欢迎使用CSDN-markdown编辑器
- CSU 1561-(More) Multiplication
- 【HDU】5208 Where is Bob 【DP】
- C++ 继承
- Preliminary understanding of bagging and boosting
- 电力系统软件应用
- Web 开发中很实用的10个效果【附源码下载】
- 特殊sql语句
- Your ways (动态规划)ACM-ICPC Asia Phuket Regional Programing Contest 2009
- Leetcode: Permutations II