zstuACM 问题 J: 简单的算术1179

《简单的》算数

zstuOJ:http://47.96.116.66/problem.php?cid=3291&pid=9
POJ:http://poj.org/problem?id=1396

中文题目:(zstuOJ)

题目描述
新的WAP门户网站的一部分也是计算器,计算具有很长数字的表达式。为了使输出看起来更好,结果的格式与通常用于手动计算的方式相同。你的任务是编写这个计算器的核心部分。 给定两个数字和请求的操作,您将计算结果并以下面指定的格式输出它:加法和减法,一个数字写在另一个数字的下方。乘法有点复杂:首先,我们对其中一个数字的每个数字计算出部分结果,然后将结果加在一起。

输入
第一行输入一个正整数T,它代表了要计算的表达式的数量。 每个表达式由包含正整数的单个行,运算符(+, - 和*之一)和第二个正整数组成。 每个正整数最多有500位数字。 每一行没有空格。如果操作是减法,则第二个数字始终低于第一个数字。 没有数字的第一位是从零开始的。

输出
对于每个表达式,打印两行给定的两个数,第二个数在第一个数的下方,两个数的最后一位数字(表示统一性)必须在同一列中对齐。将要进行的操作放在第二个数的第一位数字前面。在第二个数字下方,必须有由破折号( - )组成的水平线。对于每次加法或减法,将结果放在水平线的正下方,最后一位与两个操作数的最后一位对齐。对于每次乘法,将第一个数乘以第二个数的每个数字。从第二个数字的最后一位数的乘积开始,将部分结果放在另一个之下。每个部分结果应与相应的数字对齐,这意味着部分产品的最后一位必须与第二个数字的数字位于同一列中。任何数字都不得以任何额外的零开头。如果某一位数字为零,则这个数只有一位数——零。如果第二个数为多位数,则在所有的部分结果下打印另一条水平线,然后打印它们的总和。相对于其他约束,行的开头必须有最小数量的空格。水平线的长度总是尽可能和该水平线上方和下方的两个数字(包括运算符)的一样长。这意味着水平线和最左边的数字或者运算符(一个在下面,一个在上面)是开始于同一列。它以列中的两个数的最右边数字结束。该行既不能长于指定长度也不能短于指定长度。在每个测试用例后打印一个空白行,包括最后一个。

英文题目:(POJ)

Description

One part of the new WAP portal is also a calculator computing expressions with very long numbers. To make the output look better, the result is formated the same way as is it usually used with manual calculations.

Your task is to write the core part of this calculator. Given two numbers and the requested operation, you are to compute the result and print it in the form specified below. With addition and subtraction, the numbers are written below each other. Multiplication is a little bit more complex: first of all, we make a partial result for every digit of one of the numbers, and then sum the results together.

Input

There is a single positive integer T on the first line of input. It stands for the number of expressions to follow. Each expression consists of a single line containing a positive integer number, an operator (one of +, - and *) and the second positive integer number. Every number has at most 500 digits. There are no spaces on the line. If the operation is subtraction, the second number is always lower than the first one. No number will begin with zero.

Output

For each expression, print two lines with two given numbers, the second number below the first one, last digits (representing unities) must be aligned in the same column. Put the operator right in front of the first digit of the second number. After the second number, there must be a horizontal line made of dashes (-).

For each addition or subtraction, put the result right below the horizontal line, with last digit aligned to the last digit of both operands.

For each multiplication, multiply the first number by each digit of the second number. Put the partial results one below the other, starting with the product of the last digit of the second number. Each partial result should be aligned with the corresponding digit. That means the last digit of the partial product must be in the same column as the digit of the second number. No product may begin with any additional zeros. If a particular digit is zero, the product has exactly one digit – zero. If the second number has more than one digit, print another horizontal line under the partial results, and then print the sum of them.

There must be minimal number of spaces on the beginning of lines, with respect to other constraints. The horizontal line is always as long as necessary to reach the left and right end of both numbers (and operators) right below and above it. That means it begins in the same column where the leftmost digit or operator of that two lines (one below and one above) is. It ends in the column where is the rightmost digit of that two numbers. The line can be neither longer nor shorter than specified.

Print one blank line after each test case, including the last one.

样例输入
4
12345+67890
324-111
325*4405
1234*4
样例输出
12345
+67890
------
 80235

 324
-111
----
 213

    325
  *4405
  -----
   1625
     0
 1300
1300
-------
1431625

1234
  *4
----
4936

本文代码仅能 AC zstuOJ,不能 AC POJ(TLE)

用到的知识点:
高精度加法、减法、乘法
快速读入读出

需要注意的点:
格式非常重要,80%的时间用来调试格式问题
测试样例无法展现所有的情况

#include<iostream>
#include<cstdio>
#include<cstring> 
using namespace std;
string as,bs,cs,input;
int lena,lenb,lenc,lenmax;
char o;
inline string StringRead(){//快读
    string str;
    char s=getchar();
    while(s==' ' || s=='\n' || s=='\r'){
        s=getchar();
    }
    while(s!=' ' && s!='\n' && s!='\r'){
        str+=s;
        s=getchar();
    }
    return str;
}
inline void StringWrite(std::string str){//快写
    int i=0;
    while(str[i]!='\0'){
        putchar(str[i]),i++;
    }
}
void init_out(){//打印算式前两行(通用) 
	for(int i=0;i<lenmax-lena;i++)putchar(' ');
	StringWrite(as);
	putchar('\n');
	for(int i=0;i<lenmax-lenb-1;i++)putchar(' ');
	putchar(o);
	StringWrite(bs);
	putchar('\n');
}
string prec_minus(string minus_s1,string minus_s2){//高精度减法
	int minus_i1[1010],minus_i2[1010];
	int l1=minus_s1.length(),l2=minus_s2.length();
	string ans="";
	int len=max(l1,l2);
	
	memset(minus_i1,0,sizeof(minus_i1));
	memset(minus_i2,0,sizeof(minus_i2));

	for(int i=l1-1;i>=0;i--)//倒序赋值 char->int 
		minus_i1[l1-i-1]=minus_s1[i]-'0';
	for(int i=l2-1;i>=0;i--)
		minus_i2[l2-i-1]=minus_s2[i]-'0';
	for(int i=0;i<len;i++){
		minus_i1[i]-=minus_i2[i];
		if(minus_i1[i]<0){
			minus_i1[i]+=10;
			minus_i1[i+1]--;
		}
	}

	while(minus_i1[len-1]==0 and len>1)
		len--;//去0 
		
	for(int i=len-1;i>=0;i--)
		ans=ans+char(minus_i1[i]+'0');
	return ans;
}
string prec_plus(string plus_s1,string plus_s2){//高精度加法
	int plus_i1[1010],plus_i2[1010];
	int l1=plus_s1.length(),l2=plus_s2.length();
	string ans="";
	int len=max(l1,l2);
	
	memset(plus_i1,0,sizeof(plus_i1));
	memset(plus_i2,0,sizeof(plus_i2));

	for(int i=l1-1;i>=0;i--)//倒序赋值 char->int 
		plus_i1[l1-i-1]=plus_s1[i]-'0';
	for(int i=l2-1;i>=0;i--)
		plus_i2[l2-i-1]=plus_s2[i]-'0';
	for(int i=0;i<len;i++){
		plus_i1[i]+=plus_i2[i];
		plus_i1[i+1]+=plus_i1[i]/10;//进位
		plus_i1[i]%=10;
	}
	if(plus_i1[len]!=0) len++;//判断首位是否进位 

	while(plus_i1[len-1]==0 and len>1)
		len--;//去0 
		
	for(int i=len-1;i>=0;i--)
		ans=ans+char(plus_i1[i]+'0');
	return ans;
}
string prec_multiply(string multiply_s1,string multiply_s2){//高精度乘法 
	int multiply_i1[1010],multiply_i2[1010],multiply_i3[1010];
	int l1=multiply_s1.length(),l2=multiply_s2.length();
	string ans="";
	int len=(l1+l2);
	
	memset(multiply_i1,0,sizeof(multiply_i1));
	memset(multiply_i2,0,sizeof(multiply_i2));
	memset(multiply_i3,0,sizeof(multiply_i3));
	
	for(int i=l1-1;i>=0;i--)//倒序赋值 char->int 
		multiply_i1[l1-i-1]=multiply_s1[i]-'0';
	for(int i=l2-1;i>=0;i--)
		multiply_i2[l2-i-1]=multiply_s2[i]-'0';
		
	for(int i=0;i<l1;i++){
		for(int j=0;j<l2;j++){
			multiply_i3[i+j]+=multiply_i1[i]*multiply_i2[j];
			multiply_i3[i+j+1]+=multiply_i3[i+j]/10;
			multiply_i3[i+j]%=10;
		}
	}

	while(multiply_i3[len-1]==0 and len>1)
		len--;//去0 
		
	for(int i=len-1;i>=0;i--)
		ans=ans+char(multiply_i3[i]+'0');
	return ans;
}
void Plus_Minus(){//加减法 
	init_out();
	for(int i=0;i<lenmax-max(lenb+1,lenc);i++)putchar(' ');
	for(int i=0;i<max(lenb+1,lenc);i++)putchar('-');
	putchar('\n');
	
	for(int i=0;i<lenmax-lenc;i++)putchar(' ');
	StringWrite(cs);
	putchar('\n');
}
void Multiply(){//乘法 
	init_out();
	string t;
	
	t=bs[lenb-0-1];
	t=prec_multiply(t,as);
	for(int i=0;i<lenc-max(lenb+1,(int)t.length());i++)putchar(' ');
	for(int i=0;i<max(lenb+1,(int)t.length());i++)putchar('-');
	putchar('\n');
	
	for(int i=0;i<lenb;i++){
		t=bs[lenb-i-1];
		t=prec_multiply(t,as);
		for(int j=0;j<lenmax-t.length()-i;j++)putchar(' ');
		StringWrite(t);
		putchar('\n');
	}
	if(lenb>1){
		for(int i=0;i<lenmax-lenc;i++)putchar(' ');
		for(int i=0;i<lenc;i++)putchar('-');
		putchar('\n');
		for(int i=0;i<lenmax-lenc;i++)putchar(' ');
		StringWrite(cs);
		putchar('\n');
	}
}
void init(){//初始化两个算数as,bs 
	as="";bs="";cs="";
	int i,j;
    for(i=0;i<input.length();i++){
    	if(input[i]=='+' or input[i]=='-' or input[i]=='*'){
    		o=input[i];
    		break;
		}
	}
	for(j=0;j<i;j++)
    	as+=input[j];
	for(j=i+1;j<input.length();j++)
    	bs+=input[j];
	lena=as.length();
	lenb=bs.length();
}
int main(){
	int t,a,b;
	cin>>t;
    while(t--){
    	input=StringRead();
    	init();
    	if(o=='+'){
    		cs=prec_plus(as,bs);
			lenc=cs.length();
    		lenmax=max(lena,max(lenb+1,lenc));
    		Plus_Minus();
    		putchar('\n');
		}
		if(o=='-'){
			cs=prec_minus(as,bs);
			lenc=cs.length();
			lenmax=max(lena,max(lenb+1,lenc));
			Plus_Minus();
			putchar('\n');
		}
		if(o=='*'){
			cs=prec_multiply(as,bs);
			lenc=cs.length();
			lenmax=max(lena,max(lenb+1,lenc));
			Multiply();
			putchar('\n');
		}
	}
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值