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.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

题目很简单,说一下可能的wa点:
1. 末尾不要有空格。
2. 两项相加可能为零,此时要删除此项。
代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node *ptonext;
typedef ptonext ploy, list;
struct node{
float n;
int e;
ptonext next;
ptonext pro;
};
list A, B;
list res;
ptonext A_head, A_tail;
ptonext B_head, B_tail;
ptonext r_head, r_tail;
int cnt;
list init()
{
ptonext T;
T = (list)malloc(sizeof(list));
T->n = ;
T->e = ;
T->pro = NULL;
T->next = NULL;
return T;
}
void caculate()
{
ptonext a, b, r;
ptonext T;
a = A_head;
b = B_head;
r = r_head;
a = a->next;
b = b->next;
while(a != A_tail && b != B_tail)
{
if(a->e == b->e){
T = (ptonext)malloc(sizeof(ptonext));
T->e = a->e;
T->n = a->n + b->n;
if(T->n == ){
a = a->next;
b = b->next;
continue;
}
T->pro = r;
r->next = T;
r = r->next;
a = a->next;
b = b->next;
cnt++;
}
else if(a->e > b->e){
T = (ptonext)malloc(sizeof(ptonext));
T->e = a->e;
T->n = a->n;
r->next = T;
T->pro = r;
r = r->next;
a = a->next;
cnt++;
}
else{
T = (ptonext)malloc(sizeof(ptonext));
T->e = b->e;
T->n = b->n;
r->next = T;
T->pro = r;
r = r->next;
b = b->next;
cnt++;
}
}
while(a != A_tail){
T = (ptonext)malloc(sizeof(ptonext));
T->e = a->e;
T->n = a->n;
r->next = T;
T->pro = r;
r = r->next;
a = a->next;
cnt++;
}
while(b != B_tail){
T = (ptonext)malloc(sizeof(ptonext));
T->e = b->e;
T->n = b->n;
r->next = T;
T->pro = r;
r = r->next;
b = b->next;
cnt++;
}
r->next = r_tail;
r_tail->pro = r;
}
int main()
{
int k;
float n;
int e;
A = init();
B = init();
res = init();
A_tail = init();
B_tail = init();
r_tail = init();
A_head = A;
B_head = B;
r_head = res;
cnt = ;
scanf("%d", &k);
while(k--){
scanf("%d %f", &e, &n);
ptonext T;
T = (ptonext)malloc(sizeof(ptonext));
T->n = n;
T->e = e;
A->next = T;
T->pro = A;
A = A->next;
}
A->next = A_tail;
A_tail->pro = A;
scanf("%d", &k);
while(k--){
scanf("%d %f", &e, &n);
ptonext T;
T = (ptonext)malloc(sizeof(ptonext));
T->n = n;
T->e = e;
B->next = T;
T->pro = B;
B = B->next;
}
B->next = B_tail;
B_tail->pro = B;
caculate();
printf("%d", cnt);
while(res->next != r_tail){
printf(" %d %.1f", res->next->e, res->next->n);
res = res->next;
}
printf("\n");
// system("pause");
return ;
}

1002 A+B for Polynomials (PAT (Advanced Level) Practice)的更多相关文章

  1. PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...

  2. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  3. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  4. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  5. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  6. PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...

  7. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  8. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  9. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

随机推荐

  1. YTU 2503: 大斐波那契数列

    2503: 大斐波那契数列 时间限制: 1 Sec  内存限制: 200 MB 提交: 974  解决: 400 题目描述 斐波那契数列,又称黄金比例数列,指的是这样一个数列:0.1.1.2.3.5. ...

  2. go语言---reflect

    go语言---reflect https://blog.csdn.net/cyk2396/article/details/78902953 一.reflect的使用: import ( "f ...

  3. ODB(C++ ORM)用Mingw的完整编译过程

    用mingw官方的GCC4.7.2编译libodb后,并用odb compiler对hello示例生成odb的"包裹"代码,编译链接总是不能通过,下面是编译example/hell ...

  4. L1范数和L2范数

    给定向量x=(x1,x2,...xn)L1范数:向量各个元素绝对值之和L2范数:向量各个元素的平方求和然后求平方根Lp范数:向量各个元素绝对值的p次方求和然后求1/p次方L∞范数:向量各个元素求绝对值 ...

  5. bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声【单调栈】

    先考虑只能往一边传播,最后正反两边就行 一向右传播为例,一头牛能听到的嚎叫是他左边的牛中与高度严格小于他并且和他之间没有更高的牛,用单调递减的栈维护即可 #include<iostream> ...

  6. [App Store Connect帮助]四、添加 App 图标、App 预览和屏幕快照(1)App Store 图标、App 预览和屏幕快照概述

    您可以为您的 App Store 产品页提供有关您 App 的 App Store 图标.三个 App 预览和十张屏幕快照. App Store 图标 您必须提供一个 App Store 图标,用于在 ...

  7. C#常量知识整理

    整数常量 整数常量可以是十进制.八进制或十六进制的常量.前缀指定基数:0x 或 0X 表示十六进制,0 表示八进制,没有前缀则表示十进制. 整数常量也可以有后缀,可以是 U 和 L 的组合,其中,U ...

  8. Windows平台下Oracle 11g R2监听文件日志过大,造成客户端无法连接的问题处理

    近期部署在生产环境的应用突然无法访问,查看应用日志发现无法获取数据库连接. SystemErr R Caused by: oracle.net.ns.NetException: The Network ...

  9. 03—AOP基本配置

  10. 树莓派zero_w设置中文(已成功)

    树莓派默认是采用英文字库的,而且系统里没有预装中文字库,所以即使你在locale中改成中文,也不会显示中文,只会显示一堆方块.因此需要我们手动来安装中文字体. 好在有一个中文字体是免费开源使用的.ss ...