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 ...
随机推荐
- 韩顺平Oracle笔记
韩顺平Oracle笔记 分类: DataBase2011-09-07 10:24 3009人阅读 评论(0) 收藏 举报 oracle数据库sqljdbcsystemstring 目录(?)[-] ...
- python re正则表达式模块
模块的的作用主要是用于字符串和文本处理,查找,搜索,替换等 复习一下基本的正则表达式吧 .:匹配除了换行符以为的任意单个字符 *:匹配任意字符,一个,零个,多个都能匹配得到 俗称贪婪模式 +:匹配 ...
- xargs 主要用于不支持管道的shell命令*****
变量置换,主要用于不支持管道的shell命令,如:rm.sed等,但有些命令需要占位符“{}”需要注意.比如:删除文件- ls|xargs -i rm -rf {} 文件改名- ls|xargs ...
- 杂项-公司:Sun
ylbtech-杂项-公司:Sun Sun Microsystems是IT及互联网技术服务公司(已被甲骨文收购)Sun Microsystems 创建于1982年.主要产品是工作站及服务器.1986年 ...
- bzoj 1093: [ZJOI2007]最大半连通子图【tarjan+拓扑排序+dp】
先tarjan缩成DAG,然后答案就变成了最长链,dp的同时计数即可 就是题面太唬人了,没反应过来 #include<iostream> #include<cstdio> #i ...
- Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)
P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...
- 慕课网6-4 编程练习:jQuery选择器中的过滤器
6-4 编程练习 结合所学的jQuery过滤器知识,实现如下图所示的隔行换色效果 任务 使用jQuery的.css()方法设置样式,语法css('属性 '属性值') 使用:odd和:even过滤器实现 ...
- vue中sync,v-model----双向数据绑定
需求:父子组件同步数据 实现方式:sync或者v-model 一.sync 官网:https://cn.vuejs.org/v2/guide/components-custom-events.html ...
- elasticsearch 查询优化
首先对不必要的字段不做分词也就是不做索引,禁止内存交换 1.shard 一个Shard就是一个Lucene实例,是一个完整的搜索引擎. 分片数过多会导致检索时打开比较多的文件,多台服务器之间通讯成本加 ...
- 第3章 DOM
1.节点,dom有3种节点,元素节点,文本节点,属性节点 2.元素节点是dom的原子,所有的属性节点和文本节点都被元素包含,但并不是所有的元素都包含他们 3.继承,节点树上的元素将继承父元素的样式和属 ...