首页 > 专题系列 > Java解POJ > POJ 1555 Polynomial Showdown [解题报告] Java
2013
11-09

POJ 1555 Polynomial Showdown [解题报告] Java

Polynomial Showdown

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

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

样例输出:

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

解题代码:

//* @author: [email protected]
import java.util.Scanner;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
   while(in.hasNext())
   {
	StringBuffer bb=new StringBuffer();
	int[] a=new int[9];
	boolean b=true;
	for(int i=0;i< 9;i++)
		a[i]=in.nextInt();
	for(int i=0;i< 8;i++)
	{
	 if(a[i]!=0)
	 {
	   if(b){
		if(a[i]< 0) bb.append("-");
		b=false;
	  }
	  else if(a[i]>0) bb.append(" + ");
	  else if(a[i]< 0) bb.append(" - ");
	  bb.append((Math.abs(a[i])==1?"":Math.abs(a[i]))+"x"+((8-i)==1?"":"^"+(8-i)));
	 }
	}
	if(b)bb.append(a[8]);
	else if(a[8]>0)bb.append(" + "+Math.abs(a[8]));
	else if(a[8]< 0)bb.append(" - "+Math.abs(a[8]));
	System.out.println(bb);
      }
  }
}

  1. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  2. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。

  3. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.

  4. #include <cstdio>
    #include <algorithm>

    struct LWPair{
    int l,w;
    };

    int main() {
    //freopen("input.txt","r",stdin);
    const int MAXSIZE=5000, MAXVAL=10000;
    LWPair sticks[MAXSIZE];
    int store[MAXSIZE];
    int ncase, nstick, length,width, tmp, time, i,j;
    if(scanf("%d",&ncase)!=1) return -1;
    while(ncase– && scanf("%d",&nstick)==1) {
    for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
    std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
    for(time=-1,i=0;i<nstick;++i) {
    tmp=sticks .w;
    for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
    if(j==time) { store[++time]=tmp; }
    else { store[j+1]=tmp; }
    }
    printf("%dn",time+1);
    }
    return 0;
    }