pat甲级1002
1002. A+B for Polynomials (25)
This time, you are supposed to find A+B where A and B are two polynomials.
Input
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 (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.
Output
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<cstdio>
struct node
{
int e;//指数
double cof;//系数
}a[],b[];
double ans[]={};
int main()
{
int m,n,sum=;
scanf("%d",&m);
for(int i=;i<m;i++)
{
scanf("%d %lf",&a[i].e,&a[i].cof);
ans[a[i].e]+=a[i].cof;
}
scanf("%d",&n);
sum=m+n;
for(int i=;i<n;i++)
{
scanf("%d %lf",&b[i].e,&b[i].cof);
ans[b[i].e]+=b[i].cof;
}
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
if(a[i].e==b[j].e)
sum--;
}
} printf("%d",sum);
for(int i=;i>=;i--)
{
if(ans[i]!=)
printf(" %d %.1lf",i,ans[i]);
} return ;
}
有四个测试点过不去,应该是最后计含有非零项多项式时候少考虑了一种情况——两个指数相同的项的系数和为0
#include<cstdio>
struct node
{
int e;//指数
double cof;//系数
}a[1500],b[1500];
double ans[2002]={0};
int main()
{
int m,n,sum=0;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d %lf",&a[i].e,&a[i].cof);
ans[a[i].e]+=a[i].cof;
}
scanf("%d",&n);
sum=m+n;
for(int i=0;i<n;i++)
{
scanf("%d %lf",&b[i].e,&b[i].cof);
ans[b[i].e]+=b[i].cof;
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i].e==b[j].e)
sum--;
if(a[i].e==b[j].e&&a[i].cof+b[j].cof==0)
sum--;
}
} printf("%d",sum);
for(int i=2000;i>=0;i--)
{
if(ans[i]!=0)
printf(" %d %.1lf",i,ans[i]);
} return 0;
}
改动一段代码果然解决了问题
pat甲级1002的更多相关文章
- 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)(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
https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000 This time, you are sup ...
- PAT甲级1002水题飘过
#include<iostream> #include<string.h> using namespace std; ]; int main(){ int n1, n2; wh ...
- PAT甲级——1002 A+B for Polynomials
PATA1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomia ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...
- PAT甲级代码仓库
大道至简,知易行难.希望能够坚持刷题. PAT甲级真题题库,附上我的代码. Label Title Score Code Level 1001 A+B Format 20 1001 * 1002 A+ ...
随机推荐
- iOS 越狱后 SSH 不能连接
10.3 越狱之后,装上 OpenSSH, 但是发现连接不上,一直卡着不动,端口是通的,但是就是一直卡着.解决方法是在 Cydia 添加源 http://apt.cydiaba.cn,搜索 Dropb ...
- cookie、localstorage、sessionstorage区别
localstorage sessionsorage cookie 大小 浏览器不能保存超过300个cookie,单个服务器不能超过20个,每个cookie不能超过4k 可以达到5M 可以达到5M ...
- JanusGraph 图数据库安装小记 ——以 JanusGraph 0.3.0 为例
由于近期项目中有使用图数据的需求,经过对比,我们选择尝试使用 JanusGraph.本篇小记记录了我们安装 JanusGraph 以及需要一起集成的 Cassandra + Elasticsearch ...
- [Doctrine Migrations] 数据库迁移组件的深入解析四:集成diff方式迁移组件
场景及优势 熟悉Symfony框架之后,深刻感受到框架集成的ORM组件Doctrine2的强大之处,其中附带的数据迁移也十分方便.Doctrine2是使用Doctrine DBAL组件把代码里面的表结 ...
- 使用Selenium慢慢向下滚动页面
我正试图从航班搜索页面抓取一些数据. 此页面以这种方式工作: 你填写一个表格,然后你点击按钮搜索 – 这没关系.当您单击该按钮时,您将被重定向到包含结果的页面,这就是问题所在.这个页面连续添加结果,例 ...
- Go语言基础-序言
2018年6月,第一次接触go语言,在之后通过多本书籍渐渐了解go语言之后,开启了自己go语言全栈工程师的道路.特此记录,希望能给后学的朋友提供一个方向. 语言是一门寻寻渐进的课程,结合自己这两个月的 ...
- DotNetty学习笔记
DotNetty项目本身的示例很容易运行起来,但是具体到真实的应用场景,还是需要进一步理解DotNetty的通道处理细节,这样才能够在实际项目应用中处理具体的问题. 简单的场景下会有以下几个问题,第一 ...
- 20155217 2016-2017-2 《Java程序设计》第6周学习总结
20155217 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 InputStream与OutputStream 10.1.1串流设计的概念 Jav ...
- 20155327《Java程序设计》第二周学习总结
<Java程序设计>第二学习总结 教材学习内容总结 类型 byte(字节) shot(短整型) int(整型) long(长整型) float(浮点型) double(双精度) char( ...
- apt-get doesn't work
apt-get upgrade Reading package lists... DoneBuilding dependency tree Reading state informatio ...