云课堂程序设计入门-java语言之多项式加法

3次阅读
没有评论

题目内容:

一个多项式可以表达为x的各次幂与系数乘积的和,比如:

现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出。

程序要处理的幂最大为100。

输入格式:

总共要输入两个多项式,每个多项式的输入格式如下:

每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。第一行一定是最高幂,最后一行一定是0次幂。

注意第一行和最后一行之间不一定按照幂次降低顺序排列;如果某个幂次的系数为0,就不出现在输入数据中了;0次幂的系数为0时还是会出现在输入数据中。

输出格式:

从最高幂开始依次降到0幂,如:

  1. 2×6+3×5+12×3-6x+20

注意其中的x是小写字母x,而且所有的符号之间都没有空格,如果某个幂的系数为0则不需要有那项。

java代码

    import java.util.Scanner;
        
    public class Main {
     
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            final int SIZE =101;
             
            int[] numbers = new int[SIZE];
             
            int x;
            int y;
                        
            int num=2;
             
            while(num!=0)
            {
                x=in.nextInt();
                if(x>=0&&x<101)
                {
                    y=in.nextInt();
                    numbers[x] +=y;
                }
                 
                if(x==0)
                {
                    num--;
                }
            }
             
            boolean firstSign =true; 
             
            for(int i=numbers.length-1;i>=0;i--)
            {
                if(numbers[i]!=0){
                    if(firstSign)
                    {
                         
                        if(numbers[i]==1)
                        {
                             
                            System.out.print((i>1?"x"+i:i==1?"x":numbers[i]));
      
                        }
                        else if(numbers[i]==-1)
                        {
                             System.out.print((i>1?"-x"+i:i==1?"-x":numbers[i]));
                        }
                        else
                        {
                            System.out.print((i>1?numbers[i]+"x"+i:i==1?numbers[i]+"x":numbers[i]));
                        }
                         
                        firstSign=false;
                    }
                    else
                    {
                         
                        if(numbers[i]==1)
                        {
                             
                            System.out.print((i>1?"+x"+i:i==1?"+x":"+"+numbers[i]));
      
                        }
                        else if(numbers[i]==-1)
                        {
                             System.out.print((i>1?"-x"+i:i==1?"-x":numbers[i]));
                        }
                        else
                        {                    System.out.print((i>1?numbers[i]>0?"+"+numbers[i]+"x"+i:numbers[i]+"x"+i:i==1?numbers[i]>0?"+"+numbers[i]+"x":numbers[i]+"x":numbers[i]>0?"+"+numbers[i]:numbers[i]));
                        }                                                
                    }                                       
                }else{
                    if(i==0&&firstSign)
                    {
                        System.out.print("0");
                    }
                }                 
            }       
        }     
    }