PTA 1002 A+B for Polynomials
问题描述:
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
这题测试数据魔鬼!魔鬼!魔鬼!
同志们,作为多项式看待的时候,常数0不应该是算有1项且N1和aN1均为0吗?!我考虑了这种情况,结果有一个结果迟迟不对,哪料到把这个删了就对了,天理难容!
代码是同学发给我改的,所以比较丑。同学代码写的比较臃肿,缩进也挺不舒服的,凑合着看吧。我改的时候都没搞成tab缩进,现在自然也懒得弄了,反正AC了。
代码:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int atotal[];
int ktotal;
float btotal[]; void mix(int na,int a[],int nb,int b[],float c[],float d[])
{
int ia=,ib=;
while(ia<na&&ib<nb)
{
if (a[ia]>b[ib])
{
atotal[ktotal]=a[ia];
btotal[ktotal]=c[ia];
ia++;
}
else if (a[ia]<b[ib])
{
atotal[ktotal]=b[ib];
btotal[ktotal]=d[ib];
ib++;
}
else
{
atotal[ktotal]=a[ia];
btotal[ktotal]=c[ia]+d[ib];
ia++;ib++;
}
if (!(btotal[ktotal]>-0.05&&btotal[ktotal]<0.05)) ktotal++;
}
while(ia<na)
{
atotal[ktotal]=a[ia];
btotal[ktotal]=c[ia];
if (!(btotal[ktotal]>-0.05&&btotal[ktotal]<0.05)) ktotal++;
ia++;
}
while(ib<nb)
{
atotal[ktotal]=b[ib];
btotal[ktotal]=d[ib];
if (!(btotal[ktotal]>-0.05&&btotal[ktotal]<0.05)) ktotal++;
ib++;
}
} int main()
{
int k1,k2;
int a1[],a2[];
float b1[],b2[];
cin>>k1;
for (int i=;i<k1;i++){
cin>>a1[i];
cin>>b1[i];
}
cin>>k2;
for (int i=;i<k2;i++)
{
cin>>a2[i];
cin>>b2[i];
}
mix(k1,a1,k2,a2,b1,b2);
cout<<ktotal;
cout.precision ();
cout.setf(ios::fixed | ios::showpoint );
for (int i=;i<ktotal;i++){
cout << " " << atotal[i] << " " << round(*btotal[i])/10.0;
}
return ;
}
PTA 1002 A+B for Polynomials的更多相关文章
- PTA (Advanced Level) 1002 A+B for Polynomials
1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomials. ...
- 1002. A+B for Polynomials
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) 简单模拟
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 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 polyno ...
- PAT甲 1002. A+B for Polynomials (25) 2016-09-09 22:50 64人阅读 评论(0) 收藏
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 1002 A+B for Polynomials (25)(25 point(s))
problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...
- 【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 ...
随机推荐
- IIS停止服务 报错Connections_Refused
IIS版本6.0 无故停止工作. 重启IIS 没有效果,必须重启web服务器.出现客户端无法访问Web服务器上的站点,错误信息提示为"页面无法显示"的情况.登录服 ...
- 【转】关于apt源配置的问题
涉及的基本配置文件: apt核心配置文件集中在 /etc/apt 其中,管理软件来源的配置文件如下 sources.list // 主要软件源 so ...
- iOS开发基础--C语言简述(一)
先占个坑,回来再补 需要的运行环境,自行搜寻,工具不止一种,不详细叙述. C语言是一门非常重要的编程语言,与硬件底层直接相关,很多语言到最后的接口封装都会选择C语言,因而C语言一直很受欢迎,也务必掌握 ...
- App工程结构
在经过千辛万苦各种填坑终于安装好了Android Studio之后,在其自带的模拟器上成功运行了第一个APP(hello world),通过这个APP首先研究了一下APP基本的工程结构,从而使后面的开 ...
- hadoop完全分布式部署
1.我们先看看一台节点的hdfs的信息:(已经安装了hadoop的虚拟机:安装hadoophttps://www.cnblogs.com/lyx666/p/12335360.html) start-d ...
- #《Essential C++》读书笔记# 第二章 面向过程的编程风格
基础知识 函数必须先被声明,然后才能被调用(被使用).函数的声明让编译器得以检查后续出现的使用方式是否正确--是否有足够的参数.参数类型是否正确,等等.函数声明不必提供函数体,但必须指明返回类型.函数 ...
- JavaScript对象模型概念
1.对象的概念 JavaScript只有函数对象才有类的概念,因此创建一个对象,必须使用函数对象.(ES6中可以直接声明一个class,实质上也是一个函数对象). 函数对象的内部有[[Construc ...
- 小白的linux笔记6:关于挂载硬盘
每个硬盘,包括移动硬盘,插上之后都会有个名字,如sda,sdb,sdc.... sda,sdb等名字只与插上的顺序有关.而且重启后有可能会发生变化. 查看全部硬盘可以用fdisk -l. df -h ...
- 流程图GGEditor 之 自定义节点相关属性
自定义节点 注册 -- registerNode 我们通过以下接口往 G6 全局注册节点: // 注册节点 G6.registerNode(name, { // 绘制 draw(item) { r ...
- Maximum Element In A Stack Gym - 102222A【思维+栈】
2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest https://vjudge.net/problem ...