第4章编程题

多项式加法

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int []a=new int[101];
        for(;;){
        int b = in.nextInt();
        int c = in.nextInt();
        a[b] = c;
        if(b==0)break;
        }
        for(;;){
        int b = in.nextInt();
        int c = in.nextInt();
        a[b] += c;
        if(b==0)break;
        }
        for(int i=100;i>=0;i--){
            if(a[i]!=0){
                System.out.print(a[i]);
                if(i>=1)System.out.print("x");
                if(i>1)System.out.print(i);
                if(i!=0)System.out.print("+");
            }
        }

    }
}

你可能感兴趣的:(第4章编程题)