题解 | #多项式输出#
多项式输出
https://ac.nowcoder.com/acm/problem/16622
import java.util.Scanner;
public class Main {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean flag = true;
for(int i = 0;i<n-1;i++){
int m = sc.nextInt();
if(flag){
if(m==1){
System.out.print("x^");
System.out.print(n-i);
}
else if(m==-1){
System.out.print("-x^");
System.out.print(n-i);
}else if(m==0){
}else{
System.out.print(m);
System.out.print("x^");
System.out.print(n-i);
}
}else{
if(m==-1){
System.out.print("-x^");
System.out.print(n-i);
}
else if(m==1){
System.out.print("+");
System.out.print("x^");
System.out.print(n-i);
}else if(m==0){
}else if(m<0){
System.out.print(m);
System.out.print("x^");
System.out.print(n-i);
}else if(m>0){
System.out.print("+");
System.out.print(m);
System.out.print("x^");
System.out.print(n-i);
}
}
if(m!=0) flag = false;
}
int m= sc.nextInt();
if(flag){
if(m==1){
System.out.print("x");
}
else if(m==-1){
System.out.print("-x");
}else if(m==0){
}else{
System.out.print(m);
System.out.print("x");
}
}else{
if(m==-1){
System.out.print("-x");
}
else if(m==1){
System.out.print("+");
System.out.print("x");
}else if(m==0){
}else if(m<0){
System.out.print(m);
System.out.print("x");
}else if(m>0){
System.out.print("+");
System.out.print(m);
System.out.print("x");
}
}
if(m!=0) flag = false;
int end = sc.nextInt();
if(flag){
System.out.print(end);
}else{
if(end>0){
System.out.print("+");
System.out.print(end);
}else if(end<0){
System.out.print(end);
}else{
}
}
}
}