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 ...
随机推荐
- Python进制转换format格式化
进制转换:先介绍用传统数学方法,再介绍用python内置方法 二进制转十进制: 1101 转为十进制 1*2^(4-1)+1*2^(3-1)+0*2^(2-1)+1*2^(1-1) 即各个位拆开,乘以 ...
- 记一次全局分区索引update调优
原始SQL: CREATE OR REPLACE PROCEDURE sp_upd_suppressed_emails( A_LIMIT_BULK IN PLS_INTEGER DEFAULT 20 ...
- bash脚本计算某程序的进程数
脚本里面有时候需要判断某个程序是否启动,以及有几个进程下面用nginx来做实例 显示所有的nignx进程 ps -ef|grep nginx |grep -v grep 其中grep -v grep表 ...
- nginx检查报错:nginx: [emerg] "server" directive is not allowed here in
想检查一个配置文件是否正确,-c 指定之后发现有报错,如下: [root@op-2:~# nginx -t -c /etc/nginx/conf.d/default.conf nginx: [emer ...
- Hook基本知识
一.什么是HOOK(钩子) Windows系统,建立在事件驱动机制上,就是整个系统都是通过消息传递实现的.hook(钩子)是一种特殊的消息处理机制,它可以监视系统或者进程中的各种事件消息,截获发往目标 ...
- 2、electron进程
electron核心我们可以分成2个部分,主进程和渲染进程. 主进程: 主进程连接着操作系统和渲染进程,可以把她看做页面和计算机沟通的桥梁. Electron 运行 package.json 的 ma ...
- 杂项-桌面应用程序:Windows Live Writer(WLW)
ylbtech-杂项-桌面应用程序:Windows Live Writer(WLW) Windowslive Writer 即(WLW) 是一个免费的桌面应用程序,您可以使用它轻松发布丰富的内容到您的 ...
- centos6.5+jdk1.7+mysql5.6+tomcat8.0部署jpress
前言:此篇记录在linux下搭建环境部署jpress,mysql使用的是源码安装 1.准备 2.安装 3.部署 1.准备 a.准备centos6.5服务器环境 mysql-5.6.19.tar.gz ...
- C# Console.WriteLine堵塞进程
最近在项目中控制台为了调试使用Console.WriteLine(),发现在高并发的情况下会出现假锁状态,断点调试发现卡在Console.WriteLine那.需要进行一个键盘输入才可以继续. 关于C ...
- virtualbox压缩虚拟机硬盘文件vhd
命令如下: VBoxManage modifyhd D:\pc1\pc1.vhd --compact 当提示以下内容时,将整个虚拟机文件夹拷贝盘符根目录下,将[D:\pc1\pc1.vhd]改为相应 ...