博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分)
阅读量:3905 次
发布时间:2019-05-23

本文共 1857 字,大约阅读时间需要 6 分钟。

 

本来想在原来做的pta博客上再附上java实现的代码,发现这道题没写博客,那只好补发一波了,hhh

题目:

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 N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤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.

Sample Input:

2 1 2.4 0 3.22 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

 题意:

 多项式计算,给出两个多项式的项数,然后按照指数递减的顺序给出系数和指数。输出最后的项数和各个项的系数和指数。

 思路:

其实就是类似于链表合并操作,坑点是注意系数为0不能出现在结果中。

C++代码:

#include 
using namespace std;const int maxn=1e3+5;int n,m;struct pol { int zhi; double xi;};vector
ans;pol a[maxn],b[maxn];int numa=0,numb=0;int main(){ scanf("%d",&n); for (int i=0;i
=numb)||(a[i].zhi>b[j].zhi)) { pol x; x.zhi=a[i].zhi; x.xi=a[i].xi; ans.push_back(x); i++; } else if((i>=numa)||(b[j].zhi>a[i].zhi)) { pol x; x.zhi=b[j].zhi; x.xi=b[j].xi; ans.push_back(x); j++; } else if(i

Java 代码:

import java.util.ArrayList;import java.util.List;import java.util.Scanner;/** * @classname: Adv1002 * @description: TODO * Author : asus * Date : 2019/10/24 20:02 * Version: 1.0 **/public class Main {    public static void main(String[] args) {        Scanner sc= new Scanner(System.in);        int num1 = sc.nextInt();        List
list1 = new ArrayList<>(); List
list2 = new ArrayList<>(); List
rs = new ArrayList<>(); int re0 = 0; for (int i=0;i

 

转载地址:http://hdaen.baihongyu.com/

你可能感兴趣的文章
67. 二进制求和
查看>>
125. 验证回文串
查看>>
168. Excel表列名称
查看>>
400. 第N个数字
查看>>
209. 长度最小的子数组
查看>>
145. 二叉树的后序遍历
查看>>
2. 两数相加
查看>>
3. 无重复字符的最长子串
查看>>
5. 最长回文子串
查看>>
4. 两个排序数组的中位数
查看>>
23. 合并K个元素的有序链表
查看>>
6. Z字形转换
查看>>
8. 字符串转整数(atoi)
查看>>
15. 三数之和
查看>>
16. 最接近的三数之和
查看>>
18. 四数之和
查看>>
22. 括号生成
查看>>
24. 两两交换链表中的节点
查看>>
71. 简化路径
查看>>
77. 组合
查看>>