第4章编程题

2018-02-04  本文已影响0人  我是阿喵酱

多项式加法

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("+");
            }
        }

    }
}
上一篇下一篇

猜你喜欢

热点阅读