POJ 1555: Polynomial Showdown
——复杂模拟
原题传送门
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:
- Terms must appear in decreasing order of degree.
- Exponents should appear after a caret “^”.
- The constant term appears as only the constant.
- Only terms with nonzero coefficients should appear, unless all terms have zero coefficients in which case the constant term should appear.
- The only spaces should be a single space on either side of the binary + and - operators.
- 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.
- 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.
- 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.
Data
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
思路
这是一道模拟题.
通常为了增大难度,出题者会设置各种各样的限制条件,来增大代码量.( 不理解的可以去做一下 魔方 )
本题也不例外.
本来,不加任何限制的多项式,可以写成这样:
for (int i=8;i>=1;i--)
printf("+%dx^%d",a[i],i);
其中a[i]是第 i 项的系数.
题目这样繁杂的性质,让我们无从下手.
所谓复杂模拟,其实是指代码量.
对于这样的题目,我们的办法是:
忍了! 不忍还能怎么办
毕竟这是模拟题,一般而言找不到捷径.
所以可以一个条件一个条件的写代码.
当然,如果是赶时间,不宜做这类题目 ?
但是换句话说,这类题目也是对代码静态查错的很好训练.
比如说,加上8个条件,需要静态查错的代码就变成了:
Code
//已经AC,不必查错
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int a[15];
bool output(int c,int d)
{
if (c==0) return false;
else if (c==-1) printf(" - x^%d",d);
else if (c==1) printf(" + x^%d",d);
else if (c>1) printf(" + %dx^%d",c,d);
else if (c<-1) printf(" - %dx^%d",-c,d);
return true;
}
int main()
{
while (scanf("%d",&a[8])!=EOF)
{
bool all_zero=true;
for (int i=7;i>=0;i--) scanf("%d",&a[i]);
for (int i=1;i<=8;i++)
if(a[i]!=0) all_zero=false;
if (all_zero)
{
printf("%d\n",a[0]);
continue;
}
int k=9,h=9;
while (a[k]==0) k--,h--;
if (h==1)
{
if (a[1]==-1) printf("-x");
else if (a[1]==1) printf("x");
else if (a[1]>1) printf("%dx",a[1]);
else if (a[1]<-1) printf("%dx",a[1]);
if (a[0]!=0)
{
if (a[0]>0) printf(" + %d",a[0]);
else printf(" - %d",-a[0]);
}
puts("");
continue;
}
if (a[k]==-1) printf("-x^%d",k);
else if (a[k]==1) printf("x^%d",k);
else if (a[k]>1) printf("%dx^%d",a[k],k);
else if (a[k]<-1) printf("-%dx^%d",-a[k],k);
for (int i=k-1;i>=2;i--)
output(a[i],i);
if (a[1]==-1) printf(" - x");
else if (a[1]==1) printf(" + x");
else if (a[1]>1) printf(" + %dx",a[1]);
else if (a[1]<-1) printf(" - %dx",-a[1]);
if (a[0]!=0)
{
if (a[0]>0) printf(" + %d",a[0]);
else printf(" - %d",-a[0]);
}
puts("");
}
return 0;
}
感谢奆老关注 qwq ?