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+ ...
随机推荐
- CentOS6安装各种大数据软件 第七章:Flume安装与配置
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- Linux基础命令之关机,重启,注销
shutdown 此命令用来安全关闭或重启Linux系统,系统在关闭之前会通知所有的登录用户,系统即将关闭,此时所有新用户都不可以登录. 以下截取man手册的内容(man shutdown): NAM ...
- (1)linux和oracle---环境搭建
对linux和oracle一直是敬而远之,稍微有些了解.无奈由于工作需要这次要硬着头皮上了!@#!@@#$%^^ 对于重windows用户的我来说,简直是万种折磨. 算是做个记录吧,一定要坚持下去. ...
- tomcat 远程debug配置,教你远程调试代码,解决线上故障
IDEA远程DEBUG Tomcat很简单,配置如下: 1.修改tomcat服务器配置 打开tomcat/bin/catalina.sh 在空白处添加如下参数 CATALINA_OPTS=&quo ...
- mybatis xml文件 -- 报错 invalid comparison: java.time.LocalDateTime and java.lang.String
原配置写法:<if test="assetFlaw.handleTime != null and assetFlaw.handleTime != ''"> and af ...
- vue-cli 3.0 豆瓣api接口使用element做分页
记录自己的学习 大佬绕道谢谢! 豆瓣即将上映接口:https://api.douban.com/v2/movie/coming_soon 本地跨域问题 看我之前的文章:https://www.cnbl ...
- thinphp5-image图片处理类库压缩图片
使用tp5的thinkphp-image类库处理图片 使用方法手册都有,为了增加印象我自己记录一下 手册:https://www.kancloud.cn/manual/thinkphp5/177530 ...
- ACM1021:Fibonacci Again
Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) ...
- express函数参数之next
关于next主要从三点来进行说明: next的作用是什么? 我们应该在何时使用next? next的内部实现机制是什么? 1.Next的作用 我们在定义express中间件函数的时候都 ...
- PMP十五至尊图(第六版)
PMP(Project Management Professinoal)项目经理专业资格认证,由美国项目管理学会PMI(Project Management Institute)发起并组织的一种资格认 ...