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+ ...
随机推荐
- 使用xampp发现php的date()函数与本地相差7个小时
具体方法: 1. 打开php.ini 2. 搜索timezone 3. 修改为PRC 4. 回车键 5. 修改为PRC 6. 完成 没想到这么一个小问题也是一个大坑,在网上找了半天基本都是说要修改这个 ...
- 14JavaScript条件语句
条件语句用于基于不同的条件来执行不同的动作. 1.条件语句 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript 中,我们可使用以 ...
- PHP程序员的技术成长规划 第二阶段:提高阶段
第二阶段:提高阶段 (中级PHP程序员) 重点:提高针对LNMP的技能,能够更全面的对LNMP有熟练的应用.目标:能够随时随地搭建好LNMP环境,快速完成常规配置:能够追查解决大部分遇到的开发和线上环 ...
- 类似register uint32_t __regPriMask __ASM("primask");的代码分析
代码: #define __ASM __asm /*!< asm keyword for ARM Compiler */ #define __INLINE __inline /*!< in ...
- [转]IA64与X86-64的区别
原文:https://www.cnblogs.com/sunbingqiang/p/7530121.html 说到IA-64与x86-64可能很多人会比较陌生.不知道你在下载系统的时候有没有注意过,有 ...
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
- 20155210潘滢昊 2016-2017-2 《Java程序设计》第2周学习总结
20155210潘滢昊 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 %%:表示字符串中的%. %d:以十进制整数格式输出 %f:以十进制浮点式格式输出 % ...
- 20155230 2016-2017-2 《Java程序设计》第四周学习总结
20155230 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 1.使用extends进行扩充继承时private也会被继承但是子类不能在其中直接存取. 2 ...
- 20155235 2017-2018-1 《Java程序设计》第2周学习总结
20155235 2017-2018-1 <Java程序设计>第2周学习总结 教材学习内容总结 3.1类型.变量与运算符 类型 基本类型 类类型 变量 基本规则 不可以用数字作为开头,不可 ...
- Makefile与Myod
Makefile的引入 课堂测试总结 初识Makefile target ... : prerequisites ... command ... ... target也就是一个目标文件,可以是Obje ...