您的位置:首页 > 其它

zoj 1720 Polynomial Showdown

2015-05-09 17:35 387 查看
Polynomial Showdown

Time Limit: 2 Seconds Memory Limit: 65536 KB

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

题意:多项式的表达。

做法:把那些x的一次,然后x的系数为1和-1的时候系数不用输出,然后就是全部为0的时候输出0,最后处理好空格就行,细节题。

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int main()
{
int num[15];
int i,j;
int flag;
while(scanf("%d",&num[0])!=EOF)
{
for(i=1;i<9;i++)
scanf("%d",&num[i]);
flag=0;
for(i=0;i<9;i++)
{
if(num[i]!=0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("0\n");
continue;
}
if(i==8)
{
printf("%d",num[i]);
}
else if(i==7)
{
if(num[i]==1)
printf("x");
else if(num[i]==-1)
printf("-x");
else
printf("%dx",num[i]);
}
else
{
if(num[i]==1)
printf("x^%d",8-i);
else if(num[i]==-1)
printf("-x^%d",8-i);
else
printf("%dx^%d",num[i],8-i);
}
for(j=i+1;j<9;j++)
{
if(j==8)
{
if(num[j]>0)
printf(" + %d",num[j]);
if(num[j]<0)
printf(" - %d",-num[j]);
}
else if(j==7)
{
if(num[j]==1)
printf(" + x");
else if(num[j]==-1)
printf(" - x");
else
{
if(num[j]>0)
printf(" + %dx",num[j]);
if(num[j]<0)
printf(" - %dx",-num[j]);
}
}
else
{
if(num[j]==1)
printf(" + x^%d",8-j);
else if(num[j]==-1)
printf(" - x^%d",8-j);
else
{
if(num[j]>0)
printf(" + %dx^%d",num[j],8-j);
if(num[j]<0)
printf(" - %dx^%d",-num[j],8-j);
}
}
}
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: