1002. A+B for Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. PAT 甲级1002 A+B for Polynomials (25)

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  4. PAT 甲级 1002 A+B for Polynomials

    https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000 This time, you are sup ...

  5. PAT甲级1002水题飘过

    #include<iostream> #include<string.h> using namespace std; ]; int main(){ int n1, n2; wh ...

  6. 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 ...

  7. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  9. PAT甲级代码仓库

    大道至简,知易行难.希望能够坚持刷题. PAT甲级真题题库,附上我的代码. Label Title Score Code Level 1001 A+B Format 20 1001 * 1002 A+ ...

随机推荐

  1. docker-maven-plugin 完全免Dockerfile 文件

    # DockerSpringBootPlugin docker-maven-plugin 完全免Dockerfile 文件 使用docker-maven-plugin 进行完全免 Dockerfile ...

  2. 记一次学习PHP中的错误

    今天学习PHP时,回想起一段代码 <?php> $i = true; $o = true; $p = false; if($i or $o and $p){ echo '输出为'.'tru ...

  3. 嵌入式nand flash详解

    一.s3c2440启动后会将nand flash的前4K程序复制到内部的sram中,这个过程是硬件自动完成的,但是如果我们的程序远远大于4K,这个时候就需要将程序从flash拷贝到内存中来运行了. 二 ...

  4. python3 用户登录验证的小功能

    用户登录验证,记录一下,还需要修改黑名单不合理 #!/usr/bin/env python3 ''' 需求:编写登录接口 1.输入用户名和密码 2.验证用户密码成功后输出欢迎消息 3.3次没有验证通过 ...

  5. python列表的通用操作

    #'+'和'*'#+可以将两个列表拼接为一个列表my_list = [1,2,3]+[4,5,6]#*可以将列表重复指定的次数my_list = [1,2,3]*5 print(my_list) #创 ...

  6. js 修改字符串中某些字符的样式

    var str = 'abcdefghijklmnobqrstuvwxyz'; function HightLight(e){ var reg = new RegExp(e, 'g') str = s ...

  7. anaconda安装包找不到

    Anaconda作为一个工具包集成管理工具,下载python工具包是很方便的,直接敲: conda install package_name 1 但是有时候安装一个工具包(如skimage)的时候,在 ...

  8. 20155230 《Java程序设计》实验一(Java开发环境的熟悉) 实验报告

    练习题: 凯撒密码: import java.util.Scanner; import java.io.*; public class exp1 { public static void main(S ...

  9. 20155337 《Java程序设计》实验三(敏捷开发与XP实践)实验报告

    20155337 <Java程序设计>实验三(敏捷开发与XP实践)实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基 ...

  10. WPF MVVM从入门到精通8:数据验证

    原文:WPF MVVM从入门到精通8:数据验证 WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM从入门到精通3:数据绑定 WPF M ...