例1,3过了,例2,4没通过,不知道漏了哪些特殊情况
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] str = new int[101]; //幂数组
int cut = 0; //遍历离开的条件
int x = 0; //暂存最高次幂
boolean t = true;
//幂做下标,系数值做数组元素
while(cut != 2) {
int a = in.nextInt(); //幂
int b = in.nextInt(); //系数
if(t==true) {
x = a;
t = false;
}
for(int i=0;i<str.length;i++) {
if(i == a) {
str[i] += b;
}
}
//循环退出条件
if(a==0) {
cut++;
}
}
for(int i=x;0<=i;i--) {
//判断符号
if(str[i]>0&&i!=x) {
System.out.print("+");
}
//系数为不零,幂不为零和一
if(str[i]!=0&&i!=0&&i!=1) {
System.out.print(str[i]+"x"+i);
}
//幂为一
if(str[i]!=0&&i==1) {
if(str[i]==1) {
System.out.print("x");
}else {
System.out.print(str[i]+"x");
}
}
//幂为零
if(i==0) {
if(str[i]!=0) {
System.out.print(str[i]);
}
break;
}
}
}
}