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 ...
随机推荐
- leetcode 258. Add Digits——我擦,这种要你O(1)时间搞定的必然是观察规律,总结一个公式哇
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- [ZJOI 2007] 矩阵游戏
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1059 [算法] 二分图最大匹配 时间复杂度 : O(N^3) [代码] #inclu ...
- 2-6 ES6常用语法
- 命令搜索命令(whereis、which)
一.whereis命名 解释:搜索系统命令所在的位置,不能查询文件 语法:whereis 命令 -b 之查找可执行的文件在哪里 -m 只查找帮助文件 二.which 命令 解释:能搜索命令所在位置, ...
- MySQL-ProxySQL中间件(二)| Admin Schemas介绍
目录 MySQL-ProxySQL中间件(一)| ProxySQL基本概念: https://www.cnblogs.com/SQLServer2012/p/10972593.html ...
- DFS HDU 5305 Friends
题目传送门 /* 题意:每个点都要有偶数条边,且边染色成相同的两部分,问能有多少种染色方法 DFS+剪枝:按照边数来DFS,每种染色数为该点入度的一半,还有如果点不是偶数边就不DFS 这是别人的DFS ...
- 使用Oracle SQL Developer迁移MySQL至Oracle数据库
Oracle SQL Developer是Oracle官方出品的数据库管理工具.本文使用Oracle SQL Developer执行从MySQL迁移至Oracle数据库的操作. 2017年3月6日 操 ...
- 程序 从存储卡 内存卡 迁移到 SD卡
程序 从存储卡 内存卡 迁移到 SD卡 如果你想移动其他软件,在应用市场界面,点击“管理 > 应用搬家”,点击需要转移的应用旁边的“移至SD卡”即可.
- 转 js实践篇:例外处理Try{}catch(e){}
程序开发中,编程人员经常要面对的是如何编写代码来响应错误事件的发生,即例外处理(exception handlers).如果例外处理代码设计得周全,那么最终呈现给用户的就将是一个友好的界面.否则,就会 ...
- [ TJOI 2010 ] 打扫房间
\(\\\) Description 给出一个\(N\times M\) 的网格,一些格子是污点,求是否能用多个封闭的环路覆盖所有不是污点的格点. 封闭的环路覆盖的含义是,每条路径都必须是一个环,且每 ...