多项式加法——mooc《零基础学Java语言》-(浙大翁凯)第五周编程题
问题描述:
一个多项式可以表达为x的各次幂与系数乘积的和,比如:
2x6+3x5+12x3+6x+20
现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出。
程序要处理的幂最大为100。
输入格式:
总共要输入两个多项式,每个多项式的输入格式如下:
每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。第一行一定是最高幂,最后一行一定是0次幂。
注意第一行和最后一行之间不一定按照幂次降低顺序排列;如果某个幂次的系数为0,就不出现在输入数据中了;0次幂的系数为0时还是会出现在输入数据中。
输出格式:
从最高幂开始依次降到0幂,如:
2x6+3x5+12x3-6x+20
注意其中的x是小写字母x,而且所有的符号之间都没有空格,如果某个幂的系数为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; /* * 一个多项式可以表达为x的各次幂与系数乘积的和,比如: 2x6+3x5+12x3+6x+20 现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出。 程序要处理的幂最大为100。 输入格式: 总共要输入两个多项式,每个多项式的输入格式如下: 每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。第一行一定是最高幂,最后一行一定是0次幂。 注意第一行和最后一行之间不一定按照幂次降低顺序排列;如果某个幂次的系数为0,就不出现在输入数据中了;0次幂的系数为0时还是会出现在输入数据中。 输出格式: 从最高幂开始依次降到0幂,如: 2x6+3x5+12x3-6x+20 注意其中的x是小写字母x,而且所有的符号之间都没有空格,如果某个幂的系数为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 */ public class 多项式加法 { public static void main(String[] args) { // TODO Auto-generated method stub int[] a=new int[101];//用来存放第一个多项式的系数 int[] b=new int[101];//用来存放第二个多项式的系数 int[] c=new int[101];//用来存放合并多项式的系数 int mi1;//第一个多项式的幂指数 int xishu1;//第一个多项式的系数 int mi2;//第二个多项式的幂指数 int xishu2;//第二个多项式的系数 int isFirstout=1; Scanner in=new Scanner(System.in); //读入第一个多项式 do { mi1=in.nextInt(); xishu1=in.nextInt(); for(int i=0;i<a.length;i++) { if(i==mi1) { a[i]=xishu1; break; } } }while(mi1!=0); //读入第二个多项式 do { mi2=in.nextInt(); xishu2=in.nextInt(); for(int i=0;i<b.length;i++) { if(i==mi2) { b[i]=xishu2; break; } } }while(mi2!=0); //合并系数 for(int i=0;i<c.length;i++) { c[i]=a[i]+b[i]; } //输出多项式 for(int i=100;i>1;i--) { if(c[i]!=0) { if(isFirstout==1) {//当第一次输出时 if(c[i]!=1&&c[i]!=-1) //如果系数不是1和-1,直接输出系数 { System.out.print(c[i]+"x"+i); } if(c[i]==1) //如果系数为1,则不输出系数 { System.out.print("x"+i); } if(c[i]==-1) //如果系数为-1,则在前面加-号 { System.out.print("-x"+i); } isFirstout=0;//更新状态 } else //不是第一次输出时 { if(c[i]>1) //如果系数大于1,则先输出一个+ { System.out.print("+"); System.out.print(c[i]+"x"+i); } else if(c[i]<-1) //如果系数小于-1 { System.out.print(c[i]+"x"+i); } else if(c[i]==-1) //如果系数为-1 { System.out.print("-x"+i); } else if(c[i]==1) //如果系数为1 { System.out.print("+"); System.out.print("x"+i); } } } } //判断c[1] if(c[1]!=0) { if(isFirstout==1) //判断是否是第一次输出 { if(c[1]!=1&&c[1]!=-1) { System.out.print(c[1]+"x"); } else if(c[1]==1) { System.out.print("x"); } else if(c[1]==-1) { System.out.print("-x"); } isFirstout=0; } //如果不是第一次输出 else { if(c[1]>1) { System.out.print("+"); System.out.print(c[1]+"x"); } else if(c[1]<-1) { System.out.print(c[1]+"x"); } else if(c[1]==1) { System.out.print("+"); System.out.print("x"); } else if(c[1]==-1) { System.out.print("-x"); } } } //判断c[0] if(c[0]!=0) { if(isFirstout==1) //判断是否为第一次输出 { if(c[0]>0) { System.out.print(c[0]); } isFirstout=0; } //如果不是第一次输出 else { if(c[0]>0) { System.out.print("+"); System.out.print(c[0]); } if(c[0]<0) { System.out.print(c[0]); } } } else //判断输入是0 这种情况 { if(isFirstout==1) { System.out.print(c[0]); } } } }
吾生也有涯,而知也无涯