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 ...
随机推荐
- 实验报告三&&第五周总结
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) ① 统计该字符串中字母s出现的次数. ② 统计该字符串中子串“is ...
- es6 语法的
es5 中提供了 两个声明变量的方式 var function es6 中提供了 四种声明变量的方式 let const class import 现在,定义变量的关键字有 6 个了 备注:不能忽略函 ...
- Sensor在内核中的驱动框架【转】
本文转载自:http://blog.csdn.net/armfpga123/article/details/52840370 内核中对sensor的抽象:drivers/sensors/sensors ...
- lnmp源码搭建
Nginx工作原理 这里需要结合Apache的工作,对PHP文件处理过程的区别 1:Nginx是通过php-fpm这个服务来处理php文件 2:Apache是通过libphp5.so ...
- windows 把ps/2 鼠标当成ps/2键盘了
真坑口阿 https://zhidao.baidu.com/question/425134865713508932.html 电脑的PS/2鼠标接口认成键盘了 电脑主板技嘉,只有一个PS/2接口.开始 ...
- Java面试题集(71-85)
Java程序员面试题集(71-85) 摘要:这一部分主要包括了UML(统一建模语言).面向对象的设计原则(六原则一法则).GoF设计模式.企业级设计模式.JDBC(Java数据库连接).XML(可扩展 ...
- TCP的三次握手过程
TCP::传输控制协议(Transmission Control Protocol ) 是一种面相连接的.可靠的.基于字节流的 传输层通信协议. TCP是一种面相连接的协议.其显著的特点就是在 ...
- Cocos2d-X网络编程(4) Cocos2d中的网络通信协议——Socket通信
Socket,俗称网络套接字,本身并不是协议,而是一个调用接口,是对TCP/IP协议的封装和应用,.提供了一系列方法方便开发者进行网络通讯. TCP/IP协议是使用最早的通讯协议,它是传输层协议,主要 ...
- MySQL学习-基础练习题
day1 学生表操作: 1. 查询出班级205有多少个男生 2. 查询出名字为4个字的所有学生信息(编号.姓名,年龄,班级) 3. 查询出所有姓王的学生信息(编号.姓名,年龄,班级) 4. 查询出班级 ...
- Our growth depends not on how many experiences we devour, but on how manywe digest.
rot. v/n. 腐烂 vibration.n. 震动 charcoal. n 木炭 wrinkle. v. 长皱纹 geometry. n. 几何学 walnut.n. 核桃 tailor. n. ...