1、来,先看题,https://www.patest.cn/contests/pat-a-practise/1002。

2、需要注意的地方只有一个:两个多项式相加之后,系数可能为零,这些项不应该出现在输出中,也不应该算在总的项数内。

  接下来,就是一顿无脑暴力了:

  

 #include<iostream>
#include<iomanip>
using namespace std;
typedef struct {
int expo;
double coef;
} node;
int ka,kb;
node a[],b[];
int exist(node n) {
for(int i=; i<ka; i++) if(a[i].expo==n.expo) return i;
return -;
}
int main(void) {
cin>>ka;
for(int i=; i<ka; i++) cin>>a[i].expo>>a[i].coef;
cin>>kb;
for(int i=; i<kb; i++) cin>>b[i].expo>>b[i].coef;
int len=ka;
for(int i=; i<kb; i++) {
if(exist(b[i])!=-) a[exist(b[i])].coef+=b[i].coef;
else {
a[len].expo=b[i].expo;
a[len++].coef+=b[i].coef;
}
}
for(int i=; i<len; i++) {
for(int j=i+; j<len; j++) {
if(a[j].expo>a[i].expo) {
double c=a[i].coef;
int e=a[i].expo;
a[i].coef=a[j].coef;
a[i].expo=a[j].expo;
a[j].coef=c;
a[j].expo=e;
}
}
}
int count=;
for(int i=; i<len; i++) {
if(a[i].coef) count++;
}
cout<<count;
for(int i=; i<len; i++)
if(a[i].coef!=)
printf(" %d %.1lf",a[i].expo,a[i].coef);
cout<<endl;
}

  其实,只要做好“零系数”这个特殊情况的处理,就不会报错了。

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

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

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

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

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

  5. PAT 甲级 1002 A+B for Polynomials

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

  6. PAT甲级1002水题飘过

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

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

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

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

  9. PAT甲级1107. Social Clusters

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

  10. PAT甲级代码仓库

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

随机推荐

  1. Angular2开发拙见

    本文集中讲讲笔者目前使用ng2来开发项目时对其组件的使用的个人的一些拙劣的经验. 先简单讲讲从ng1到ng2框架下组件的职责与地位: ng1中的一大特色--指令,分为属性型.标签型.css类型和注释型 ...

  2. eclipse中搜狗输入法中文状态下输出的全是英文

    在eclipse中搜狗输入法变成了如图这样 在中文状态下,提示的全是中文. 查询到的解决方案: 快捷键ctrl+shift+E关闭搜狗智能英文.然而与eclipse中 Ctrl+shift+E 快捷键 ...

  3. 原创:TSP问题解决方案-----禁忌搜索算法C实现

    本文着重于算法的实现,对于理论部分可自行查看有关资料可以简略参考该博文:http://blog.csdn.net/u013007900/article/details/50379135 本文代码部分基 ...

  4. 最长回文 hdu3068(神代码)

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Linux+Apache2.4+PHP5.6+MySQL5.6源码安装步骤

    一.安装Apache 若要安装apache服务器软件,需要安装以下几个依赖软件 apr-1.4.6.tar.gz 下载地址:http://apr.apache.org/ apr-util-1.4.1. ...

  6. Flash与 Javascript 交互

    网页加载时立即调用 ExternalInterface.addCallback中定义的函数会失败,放到按键中调用正常. 推测:可能是flash对象加载时间略长,网页加载到js时,flash对象尚未初始 ...

  7. HDU5661 Claris and XOR

    我们求二进制是怎么求的呢:先看看二进制的每一位代表多大:.......32 16 8 4 2 1 假如n=10, ..... 32>n ,不要. 16>n,不要. 8<=n,要,然后 ...

  8. zoj1002 Fire Net

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  9. PL/SQL 三个小例子

    /* SQL语句 员工集合:select to_char(hiredate,'yyyy') from emp --> 光标 --> 循环--> 退出条件:notfound 变量 co ...

  10. win10 uwp 模拟网页输入

    有时候需要获得网页的 js 执行后的源代码,或者模拟网页输入,如点按钮输入文字. 如果需要实现,那么就需要用 WebView ,使用方法很简单. 首先创建一个 WebView ,接下来的所有输入都需要 ...