1002 A+B for Polynomials(25 分)
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
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.
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.
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
多项式求和
扫描二维码关注公众号,回复: 3005687 查看本文章
Answer
- 链表实现
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Collections;
/**
* Created by Boolean on 2018/8/8.
*/
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
LinkedList<Node> list = new LinkedList<Node>();
LinkedList<Node> list1 = new LinkedList<Node>();
int count = 2;
while(count>0)
{
int n = scanner.nextInt();
Node node = null;
while (n>0)
{
n--;
int Exponents = scanner.nextInt();
double Coefficients = scanner.nextDouble();
if(Coefficients!=0)
{
node = new Node(Exponents,Coefficients);
if(count==2)
list.add(node);
else list1.add(node);
}
}
count--;
}
count = 0;
int i = 0,j = 0;
LinkedList<Node> res = new LinkedList<>();
while (i<list.size()&&j<list1.size())
{
// System.out.printf("%d %d\n",i,j);
Node node1 = list.get(i);
Node node2 = list1.get(j);
int ex;double co;
if(node1.exponents==node2.exponents)
{
ex = node1.exponents;
co = node1.coefficients+node2.coefficients;
Node newNode = new Node(ex,co);
res.add(newNode);
i++;j++;
if(newNode.coefficients!=0)count++;
}
else if(node1.exponents>node2.exponents)
{
res.add(node1);i++; count++;
}
else
{
res.add(node2);j++; count++;
}
}
// System.out.printf("%d %d\n",i,j);
while (i<list.size())
{
Node node = list.get(i);
i++;
res.add(node);
if(node.coefficients!=0)count++;
}
while (j<list1.size())
{
// System.out.println(j);
Node node = list1.get(j);
j++;
res.add(node);
if(node.coefficients!=0)count++;
}
System.out.printf("%d",count);
for (Node node:res) {
if(node.coefficients!=0.0)
System.out.printf(" %d %.1f",node.exponents,node.coefficients);
}
}
static class Node{
private int exponents;
private double coefficients;
public Node(int exponents,double coefficients)
{
this.exponents = exponents;
this.coefficients = coefficients;
}
public int getExponents() {
return exponents;
}
public void setExponents(int exponents) {
this.exponents = exponents;
}
public double getCoefficients() {
return coefficients;
}
public void setCoefficients(double coefficients) {
this.coefficients = coefficients;
}
}
}
- 数组散列实现
import java.util.*;
/**
* Created by Boolean on 2018/8/8.
*/
public class Main {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n =scanner.nextInt();
double[] res = new double[1001];
int[] list = new int[1001];
int count = 0;
while (n>0)
{
int ex = scanner.nextInt();
double coe = scanner.nextDouble();
res[ex] = coe;
list[count] = ex;
count++;
n--;
}
n = scanner.nextInt();
while (n>0)
{
int ex = scanner.nextInt();
double coe = scanner.nextDouble();
if(res[ex]==0)
{
list[count] = ex;
// System.out.println(ex);
count++;
}
res[ex] = coe+res[ex];
n--;
}
// System.out.println();
Arrays.sort(list,0,count);
int res_c = 0;
for(int i=0;i<count;i++)
{
if(res[list[i]]!=0)
{
res_c++;
}
}
System.out.printf("%d",res_c);
for(int i=count-1;i>=0;i--)
{
//System.out.println(list[i]);
//
if(res[list[i]]!=0)System.out.printf(" %d %.1f",list[i],res[list[i]]);
}
}
}