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 ...
随机推荐
- JSP-Runood:Eclipse JSP/Servlet 环境搭建
ylbtech-JSP-Runood:Eclipse JSP/Servlet 环境搭建 1.返回顶部 1. Eclipse JSP/Servlet 环境搭建 本文假定你已安装了 JDK 环境,如未安装 ...
- spring-boot-configuration-processor的作用
spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了 先引入pom依赖 <d ...
- ssh使用秘钥文件连接提示WARNING: UNPROTECTED PRIVATE KEY FILE!(转载)
转自:http://www.01happy.com/ssh-unprotected-private-key-file/ 在centos 6.4下使用ssh连接远程主机时,用的是另外一个密钥,需要用-i ...
- bzoj 1642: [Usaco2007 Nov]Milking Time 挤奶时间【dp】
这不就是个n方dp吗--看了眼洛谷题解简直神仙打架 我全程没用到n-- 把休息时间并入产奶时间,注意"结束时间不挤奶",所以ei=ei+r-1,注意这个-1! 然后按r排序,设f[ ...
- bzoj3252攻略(线段树+dfs序)
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 562 Solved: 238[Submit][Status][Discuss] D ...
- 论文翻译-SELF TRAINING AUTONOMOUS DRIVING AGENT
文献地址 链接:https://pan.baidu.com/s/1gHrpnOf1FXLp9u8OJ2-oCg 提取码:y2w6 作者 Shashank Kotyan, Danilo Vasconce ...
- [Qt Creator 快速入门] 第5章 应用程序主窗口
对于日常见到的应用程序而言,许多都是基于主窗口的,主窗口中包含了菜单栏.工具栏.状态栏和中心区域等.这一章会详细介绍主窗口的每一个部分,还会涉及资源管理.富文本处理.拖放操作和文档打印等相关内容.重点 ...
- 数据结构之单链表(C实现)
list.h #ifndef LIST_H #define LIST_H #include <iostream> #include <stdio.h> #include < ...
- [译]curl_multi_info_read
curl_multi_info_read - read multi stack informationals读取multi stack中的信息 SYNOPSIS#include <curl/cu ...
- Mysql慢SQL与索引案例
写在最前 关于慢sql的开启与配置查看之前我整理的文章: http://www.cnblogs.com/hanxiaobei/p/5515624.html 前提准备: tomcat7.x mysql- ...