来自https://www.cnblogs.com/bcd589/p/6082453.html
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int []a,b;//两个数组存放两个式子
int max;//较长式子的长度
int n=in.nextInt();//幂
int x=in.nextInt();//系数
a=new int [n+1];//n+1为数组长度
a[n]=x;
do {
n=in.nextInt();
x=in.nextInt();
a[n]=x;
}while(n!=0);
n=in.nextInt();
x=in.nextInt();
b=new int [n+1];
b[n]=x;
do{
n=in.nextInt();
x=in.nextInt();
b[n]=x;
}while(n!=0);
if(a.length>b.length){max=a.length;}else {max=b.length;}
for(int i=max-1;i>=0;i--)
{
int sum=0;
if(a[i]!=0&&i<a.length)
{
sum+=a[i];
}
if(b[i]!=0&&i<b.length)
{
sum+=b[i];
}
if(sum==0) continue;
if(i!=0)
System.out.print(sum+"X"+i+"+");
else {
System.out.println(sum);
}
}
}
}