PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)
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 (,) are the exponents and coefficients, respectively. It is given that 1,0.
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct poly{
int expo;
double coef;
};
bool cmp(poly po1,poly po2){
if(po1.expo>po2.expo) return true;
return false;
}
int main(){
/**
注意点:
1.先指数,后系数
2.题意没有注明是否是递降的多项式,所以当做递降的来试试
**/
/**输入数据*/
int T;poly temp;
vector<poly> vec1;
vector<poly> vec2;
vector<poly> res;
cin>>T;
while(T--){
cin>>temp.expo;
cin>>temp.coef;
vec1.push_back(temp);
}
cin>>T;
while(T--){
cin>>temp.expo;
cin>>temp.coef;
vec2.push_back(temp);
}
/**排序,题意没说是有序的*/
sort(vec1.begin(),vec1.end(),cmp);
sort(vec2.begin(),vec2.end(),cmp);
/**计算*/
int i=,j=;
while(true){
if(vec1[i].expo==vec2[j].expo){
poly temp;
temp.expo=vec1[i].expo;
temp.coef=vec1[i].coef+vec2[j].coef;
if(temp.coef!=) res.push_back(temp);
/**coef不为0时相加!!!!!*/
i++;j++;
}else if(vec1[i].expo>vec2[j].expo){
res.push_back(vec1[i]);
i++;
}else{
res.push_back(vec2[j]);
j++;
}
if(i==(vec1.size())){
while(j!=(vec2.size())){
res.push_back(vec2[j]);
j++;
}
break;
}
if(j==(vec2.size())){
while(i!=(vec1.size())){
res.push_back(vec1[i]);
i++;
}
break;
}
if(i==(vec1.size())&&j==(vec2.size())){
break;
}
}
cout<<res.size();
for(int i=;i<res.size();i++){
printf(" %d %.1f",res[i].expo,res[i].coef);
/**注意浮点数应该是一位小数!!!!!*/
}
system("pause");
return ;
}
逻辑正确,出现了大量错误:
1.系数不为0相加未考虑
2.浮点数为一位小数未考虑
考试时应该注意
PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)的更多相关文章
- 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】1002. A+B for Polynomials (25)
1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...
- PAT甲级 1002 A+B for Polynomials (25)(25 分)
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...
- PAT 甲级1002 A+B for Polynomials (25)
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- PAT 1002 A+B for Polynomials (25分)
题目 This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: E ...
- PAT (Advanced Level) 1009. Product of Polynomials (25)
简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...
- 【PAT甲级】1002 A+B for Polynomials (25 分)
题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...
- 1002 A+B for Polynomials (25分)
This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...
- PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
随机推荐
- 使用Vue前端框架实现知乎日报app
这是:主页代码 <template> <view class="content"> <view class="uni-list"& ...
- 2018-2019-2 网络对抗技术 20165220 Exp 9 Web安全基础
2018-2019-2 网络对抗技术 20165220 Exp 9 Web安全基础 实验任务 本实践的目标理解常用网络攻击技术的基本原理,做不少于7个题目,共3.5分.包括(SQL,XSS,CSRF) ...
- Mac ssh key生成
转载https://blog.csdn.net/wangjunling888/article/details/51115659 1. 查看秘钥是否存在 打开终端查看是否已经存在SSH密钥:cd ~/. ...
- IoC有什么好处
IoC(Inversion of Control):控制反转. DI(Dependency Injection):依赖注入. 控制反转是目的,依赖注入是实现控制反转的手段. 控制反转是一种面向对象的思 ...
- vs2019安装
1.下载vs_enterprise.exe(建议下载到无中文无空格目录) ,这个很小,官网下载企业版即可 2.在当前目录cmd命令执行: vs_enterprise.exe --layout offl ...
- python - 标准库:traceback模块
traceback 模块: 允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常时解释器所做的. import traceback try: raise SyntaxErro ...
- Flink容错机制
Flink的Fault Tolerance,是在在Chandy Lamport Algorithm的基础上扩展实现了一套分布式Checkpointing机制,这个机制在论文"Lightwei ...
- 打印GC日志
所需参数如下: -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -verbose:gc -Xloggc:gc.log 会在根目录生成 gc.log 文件,里面记录 ...
- 每次进步一点点——linux expect 使用
1. 介绍 expect是建立在tcl(参见:Tcl/Tk快速入门 )基础上的一个工具,它可以让一些需要交互的任务自动化地完成.相当于模拟了用户和命令行的交互操作. 一个具体的场景:远程登陆服务器,并 ...
- python基础--函数1
# 一,为什么使用函数 # 1,可以使代码的组织结构清晰,可读性好 # 2,遇到重复的问题可以直接调用函数 # 3,功能扩展时,可直接修改,而无需每处都进行修改. # 二,函数为何物 # 函数对程序员 ...