1002 A+B for Polynomials(Java)代码很长,但思路很清晰

This time, you are supposed to find A+B where A and B are two polynomials.

这一次,你应该找到A+B,其中A和B是两个多项式。

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

每个输入文件包含一个测试用例。每种情况占用2行,每行包含一个多项式的信息:

K N1​ aN1​​ N2​ aN2​​ ... NK​ aNK​​

where K is the number of nonzero terms in the polynomial, Ni​ and aNi​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK​<⋯<N2​<N1​≤1000.

其中K是多项式中非零项的数量,Ni​和aNi​​(i=1,2,…,K)分别是指数和系数。给出了1≤K≤10,0≤NK​<⋯<N2<N1​≤1000

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

对于每个测试用例,您应该在一行中输出A和B的总和,格式与输入相同。请注意,每一行的末尾都不能有多余的空格。请精确到小数点后1位。

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

解题思路:看到多项式相加,最先想起的就是用链表解决。

注意事项:1.系数相加为0的项直接省去

                  2.保留小数点后一位

                  3.结果没有非零项的直接输出0(后面没有空格!!!)我测试出来的!!

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int k1 = sc.nextInt();
        Node head1 = new Node();
        Node temp1 = head1;
        for(int i = 0 ; i < k1;i++)
        {
            temp1.next = new Node(sc.nextInt(),sc.nextDouble());
            temp1 = temp1.next;
        }
        int k2 = sc.nextInt();
        Node head2 = new Node();
        Node temp2 = head2;
        for(int i = 0 ; i < k2;i++)
        {
            temp2.next = new Node(sc.nextInt(),sc.nextDouble());
            temp2 = temp2.next;
        }
        Node head3 = new Node();
        Node l3 = head3;
        int count = 0;
        head1 = head1.next;//省去创建表头时的空表头,下同
        head2 = head2.next;
        while(head1 != null && head2 != null)
        {
            if(head1.ex > head2.ex)
            {
                l3.next = head1;
                head1 = head1.next;
                l3 = l3.next;
                count ++;
            }
            else if(head1.ex < head2.ex)
            {
                l3.next = head2;
                head2 = head2.next;
                l3 = l3.next;
                count ++;
            }
            else
            {
                if(head1.co + head2.co != 0)
                {
                    l3.next = new Node(head1.ex ,head1.co + head2.co);
                    l3 = l3.next;
                    count ++;
                }
                head1 = head1.next;
                head2 = head2.next;
            }
        }
        if(head1 == null)
        {
            while(head2 != null)
            {
                l3.next = head2;
                head2 = head2.next;
                l3 = l3.next;
                count ++;
            }
        }
        else if(head2 == null)
        {
            while(head1 != null)
            {
                l3.next = head1;
                head1 = head1.next;
                l3 = l3.next;
                count ++;
            }
        }
        head3 = head3.next;//省去创建表头时的空表头
        if(count == 0)//没有非零项时
        {
            System.out.print(count);
            return ;
        }
        System.out.print(count + " ");
        int n = 0;//n用来控制格式
        while(head3 != null)
        {
            if(n != count - 1 )
            {
                System.out.printf("%d %.1f ",head3.ex,head3.co);
            }
            else
            {
                System.out.printf("%d %.1f\n",head3.ex,head3.co);
            }
            head3 = head3.next;
            n++;
        }
    }
}
class Node{
    int ex;//指数
    double co;//系数
    Node next;
    public Node(int ex, double co)
    {
        this.ex = ex;
        this.co = co;
    }
    public Node()
    {
    }
}

 

这个格式真的很恶心人

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值