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 ...
随机推荐
- 2.【Spring Cloud Alibaba】实现负载均衡-Ribbon
负载均衡的两种方式 如何实现负载均衡 目前已经实现让A总能找到B,如何实现负载均衡 负载均衡的两种方式 服务器端负载均衡 客户端负载均衡 使用Ribbo实现负载均衡 Ribbon是什么 ==Netfl ...
- Mac将本地文件上传到Centos7(Linux)服务器上
1.打开终端,输入命令: scp /Users/codez/Downloads/jdk-8u144-linux-x64.tar.gz root@139.224.235.xxx:/root/java/j ...
- 移动端键盘顶起遮挡输入框&offsetTop值不准问题
先上图 通常在开发中我们会遇到这样输入框被遮挡的问题,那么该怎么解决呢? 方案一(css): 首先,把置底元素设置成,在页面的底部而非屏幕的底部 .page .bottom { position ...
- javascript30--day01--Drum kit
相关视频链接:https://www.bilibili.com/video/av8481988/?p=3 Drum kit 做题思路(1)监听键盘事件 addEventListener(‘事件名’,执 ...
- 前端html,css考点
1, 内联元素,块级元素相关知识点 参考链接:https://edu.aliyun.com/a/103378 (1)置换元素 概念:浏览器根据元素的标签和属性,来决定元素的具体显示内容.<img ...
- centos下mysql中table大小写改为不敏感
项目由win系统部署到linux系统下,启动项目报错如下: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ...
- eclipse中一个项目引用另一个项目的方法
我们在开发的时候,有时候需要把一个大的项目打散,尤其是现在微服务的架构很流行,一个大的项目往往被拆成很多小的项目,而有的项目作为公共工程被独立出来,比如有个工程专门提供各种Util工具类,有的工程专门 ...
- Spring Bean自动装配有哪些方式?
Spring 容器能够自动装配 Bean .也就是说,可以通过检查 BeanFactory 的内容让 Spring 自动解析 Bean 的协作者. 自动装配的不同模式: no - 这是默认设置,表示没 ...
- Navicat Premium 12永久激活
参考:https://baijiahao.baidu.com/s?id=1644169351506023288&wfr=spider&for=pc 百度网盘:https://pan.b ...
- c#学习心得(2)
1.foreach与IEnumerable和IEnumerator的结合使用????? using System; using System.Collections; class Program { ...