2018_1_28_Polynomial Showdown_格式输出不熟的来做

Polynomial Showdown
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5628 Accepted: 1821

Description

Given the coefficients of a polynomial from degree 8 down to 0, you are to format the polynomial in a readable format with unnecessary characters removed. For instance, given the coefficients 0, 0, 0, 1, 22, -333, 0, 1, and -1, you should generate an output line which displays x^5 + 22x^4 - 333x^3 + x - 1.
The formatting rules which must be adhered to are as follows:

1. Terms must appear in decreasing order of degree.

2. Exponents should appear after a caret `"^".

3. The constant term appears as only the constant.

4. Only terms with nonzero coefficients should appear, unless all terms have zero coefficients in which case the constant term should appear.

5. The only spaces should be a single space on either side of the binary + and - operators.

6. If the leading term is positive then no sign should precede it; a negative leading term should be preceded by a minus sign, as in -7x^2 + 30x + 66.

7. Negated terms should appear as a subtracted unnegated term (with the exception of a negative leading term which should appear as described above). That is, rather than x^2 + -3x, the output should be x^2 - 3x.

8. The constants 1 and -1 should appear only as the constant term. That is, rather than -1x^3 + 1x^2 + 3x^1 - 1, the output should appear as-x^3 + x^2 + 3x - 1.

Input

The input will contain one or more lines of coefficients delimited by one or more spaces. There are nine coefficients per line, each coefficient being an integer with a magnitude of less than 1000.

Output

The output should contain the formatted polynomials, one per line.

Sample Input

0 0 0 1 22 -333 0 1 -1
0 0 0 0 0 0 -55 5 0

Sample Output

x^5 + 22x^4 - 333x^3 + x - 1
-55x^2 + 5x

Source

Mid-Central USA 1996
#include<iostream>
#include<algorithm>
#include<string> 
#include<cstring>
#include<cmath>
#include<fstream>
using namespace std; 
int a[9];
bool ff;
ofstream out;
void print(int t,int cnt,int f){
	if(!t)return;
	char c;
	ff=true;
	if(t<0)c='-';
	else c='+';
	if(!f){
		if(abs(t)==1){
			if(cnt==1){
				if(t<0)
					printf("%cx",c);
				else
					printf("x");
			}
			else if(cnt==0)
				printf("%d",t);
			else{
				if(t<0)
					printf("%cx^%d",c,cnt);
				else
					printf("x^%d",cnt);
			}
			return;
		}
		if(cnt==1)
			printf("%dx",t);
		else if(cnt==0)
			printf("%d",t);
		else 
			printf("%dx^%d",t,cnt);
		return;
	}else{
		if(abs(t)==1){
			if(cnt==1)
				printf(" %c x",c);
			else if(cnt==0)
				printf(" %c %d",c,abs(t));
			else 
				printf(" %c x^%d",c,cnt);
			return;
		}
		if(cnt==1)
			printf(" %c %dx",c,abs(t));
		else if(cnt==0)
			printf(" %c %d",c,abs(t));
		else printf(" %c %dx^%d",c,abs(t),cnt);
	}
}
int main(){
	while(scanf("%d",&a[8])!=EOF){
		for(int i=7;i>=0;i--)
		scanf("%d",&a[i]);
		int f=0;
		ff=false;
		for(int i=8;i>=0;i--){
			if(a[i]!=0&&!f){
				print(a[i],i,f);
				f=1;
				continue;
			}
			print(a[i],i,f);
		}
		if(!ff){
			printf("0");
		}
		puts("");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值