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 ...
随机推荐
- 在SOUI3.0中使用数值动画
上一篇介绍了插值动画,插值动画是直接作用于窗口对象的. 数值动画则可以作用于任何对象. SOUI内置了3种数值类型的动画,分别是SIntAnimator, SFloatAnimator, SColor ...
- leetcode 206 反转链表 Reverse Linked List
C++解法一:迭代法,使用前驱指针pre,当前指针cur,临时后继指针nxt: /** * Definition for singly-linked list. * struct ListNode { ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- PHP面向对象-设计模式 单例模式 简单工厂模式 工厂方法模式
1.单例模式 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例.即一个类只有一个对象实例. 要实现每一个类只有一个实例,就需 ...
- 三:flask-配置文件的两种方式
项目中,配置的参数一般采用配置文件的形式,方便统一管理 第一种方式:模块的形式:使用app.config.from_object(config)的方式加载配置文件,此方式需要导入配置文件视为模块 第二 ...
- Delphi加密解密算法
// 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...
- Golang http post error : http: ContentLength=355 with Body length 0
参考:https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0 问 ...
- 正则表达式分组(Grouping)
一 捕获型 (x) 匹配 x ,并且捕获匹配项 const regExp = /(\w+)\s+(\d+)/; const str = 'Android 8'; str.replace(regExp, ...
- Java之类的继承
说起来Java的类,不得不说以下几个方面:继承.转型.重写.多态和接口. 今天来说一说继承,转型和重写几个方面: 继承(extends)即子类继承父类,就好比玻璃杯.保温杯等子类继承了杯子这个父类,子 ...
- numpy2
1.通用函数,是一种在ndarray数据中进行逐元素操作的函数.某些函数接受一个或多个标量数值,并产生一个或多个标量结果,通用函数就是对这些函数的封装. 1.常用的一元通用函数有:abs\fabs s ...