1002 A+B for Polynomials (PAT (Advanced Level) Practice)
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. It is given that 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.
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)的更多相关文章
- 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 ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT (Advanced Level) Practice 1001-1005
PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- 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 ...
- 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 ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- 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 ...
随机推荐
- 【Codevs 3115】高精度练习之减法
http://codevs.cn/problem/3115/ 板子题~ // <H.cpp> - Sun Oct 9 12:58:23 2016 // This file is made ...
- 杂项-Java:JSP
ylbtech-杂项-Java:JSP 1.返回顶部 1. JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它是由Sun Micros ...
- E20171229-hm
specification n. 规格; 说明书; 详述;
- sql server数据库占用cpu太大,使用sys.dm_exec_query_stats查询优化
查询sql语句占用 CPU详细信息: SELECT (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , ( (CASE WHE ...
- P4097 [HEOI2013]Segment
传送门 简单来说就是对于每条线段,先把它拆成\(O(logn)\)条,然后对于每一条再\(O(logn)\)判断在所有子区间的优劣程度 //minamoto #include<bits/stdc ...
- [App Store Connect帮助]四、添加 App 图标、App 预览和屏幕快照(5)移除 App 预览或屏幕快照
您可以随时移除 App 预览,但仅可在 App 状态为可编辑时才能移除屏幕快照.要了解可编辑的状态,请前往 App 状态. 必要职能:“帐户持有人”职能.“管理”职能.“App 管理”职能或“营销”职 ...
- 使用nginx加zuul配置
配置文件 $ ls -lrt -rw-r--r-- 1 root root 826 May 10 10:56 nginx.conf $ pwd /etc/nginx 增加配置 在http {}里 up ...
- [C陷阱和缺陷] 第5章 库函数
有关库函数的使用,我们能给出的最好建议是尽量使用系统头文件,当然也可以自己造轮子,随个人喜好.本章将探讨某些常用的库函数,以及编程者在使用它们的过程中可能出错之处. 5.1 返回整数的getc ...
- POJ 2194 2850 计算几何
题意: 给你了n个圆,让你摞起来,问顶层圆心的坐标 (数据保证间隔两层的圆不会挨着) 思路: 按照题意模拟. 假设我们已经知道了一层两个相邻圆的坐标a:(x1,y1)和b:(x2,y2) 很容易求出来 ...
- svn报错:Cannot negotiate authentication mechanism
在使用eclipse的svn插件连接osc的代码仓库时候,发生了以下错误: Cannot negotiate authentication mechanismsvn: Unable to connec ...