題目內容:一個多項式可以表達為x的各次冪與系數(shù)乘積的和,比如:2x6+3x5+12x3+6x+20
現(xiàn)在魔慷,你的程序要讀入兩個多項式雹舀,然后輸出這兩個多項式的和,也就是把對應的冪上的系數(shù)相加然后輸出煌贴。
程序要處理的冪最大為100御板。
輸入格式:總共要輸入兩個多項式,每個多項式的輸入格式如下:
每行輸入兩個數(shù)字牛郑,第一個表示冪次怠肋,第二個表示該冪次的系數(shù),所有的系數(shù)都是整數(shù)淹朋。第一行一定是最高冪笙各,最后一行一定是0次冪。
注意第一行和最后一行之間不一定按照冪次降低順序排列础芍;如果某個冪次的系數(shù)為0杈抢,就不出現(xiàn)在輸入數(shù)據中了;0次冪的系數(shù)為0時還是會出現(xiàn)在輸入數(shù)據中仑性。
輸出格式:從最高冪開始依次降到0冪惶楼,如:2x6+3x5+12x3-6x+20
注意其中的x是小寫字母x,而且所有的符號之間都沒有空格,如果某個冪的系數(shù)為0則不需要有那項歼捐。
輸入樣例:
6 2
5 3
3 12
1 6
0 20
6 2
5 3
2 12
1 6
0 20
輸出樣例:4x6+6x5+12x3+12x2+12x+40
時間限制:500ms內存限制:32000kb
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0;
int[] list = new int[101];
do {
int index = in.nextInt();
int value = in.nextInt();
list[index] += value;
if (index == 0) {
count++;
}
} while(count < 2);
boolean flag=true;
for (int i = 100;i >= 0;i--) {
if (list[i] != 0) {
if (!flag && list[i] > 0)
System.out.print("+");
if (i == 0)
System.out.print(list[i]);
if(i > 1 && list[i] != 1)
System.out.print(list[i] + "x" + i);
if(i > 1 && list[i] == 1)
System.out.print("x" + i);
if(i == 1 && list[i] != 1)
System.out.print(list[i] + "x");
if(i == 1 && list[i] == 1)
System.out.print("x");
flag = false;
}
}
if(flag)
System.out.print(0);
}
}