PAT (Advanced Level) Practise 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
大意是:
多项式加法
共两行输入数据,每行输入数据包括:K N1 aN1 N2 aN2 ... NK aNK,其中K是在多项式非零项的数量,Ni和aNi分别是指数和系数。
1<=K<=10,0<=NK<... < N2 < N1<=1000.
解题思路
- 数组下标作为次数,数值存储系数和;
- 边读入边计算最终非零项个数。
注意点:
- 系数和为0的项不计算在内;
- 输出结果不能有多余空格。
代码
错误版本
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int i,j,n,m,s,t;
float a[1001],k;
memset(a,0,sizeof(a));
cin>>n;
for (i=0;i<n;i++)
{
cin>>j>>k;
a[j]=k;
}
t=n;
cin>>n;
for (i=0;i<n;i++)
{
cin>>j>>k;
if (a[j]==0) t++;
a[j]+=k;
if (a[j]==0) t--;
}
cout<<t<<' ';
n=t;
t=0;
for (i=1000;i>=0;i--)
{
if (a[i]!=0)
{
if (t<n)
{
printf("%d %.1f ",i,a[i]);
t++;
}
else printf("%d %.1f",i,a[i]);
}
}
return 0;
}
评测结果为全部格式错误。经过检查,防止最后多出空格的做法错了 不应该偷懒。。t的初始值应该设为1,或改变if的判断条件。
正确版本(用了更简便的输出)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int i,j,n,m,s,t;
float a[1001],k;
memset(a,0,sizeof(a));
cin>>n;
for (i=0;i<n;i++)
{
cin>>j>>k;
a[j]=k;
}
t=n;
cin>>n;
for (i=0;i<n;i++)
{
cin>>j>>k;
if (a[j]==0) t++;
a[j]+=k;
if (a[j]==0) t--;
}
cout<<t;
for (i=1000;i>=0;i--)
if (a[i]!=0) printf(" %d %.1f",i,a[i]);
return 0;
}
提交记录
错误版本

修改后

PAT (Advanced Level) Practise 1002 解题报告的更多相关文章
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- PAT (Advanced Level) Practise 1003 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题 ...
- PAT (Advanced Level) Practise 1001 解题报告
GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
- PAT (Advanced Level) Practise:1002. A+B for Polynomials
[题目链接] This time, you are supposed to find A+B where A and B are two polynomials. Input Each input f ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
随机推荐
- Nginx的核心功能及应用实战
反向代理功能及配置: 反向代理(Reverse Proxy)方式是指以代理服务器来接受 internet 上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给interne ...
- 解决:sudo: pip: command not found
1-问题:Ubuntu下执行sudo pip install package-name 出现 sudo: pip: command not found 的问题. 2-原因:编译sudo的时候加入了–w ...
- Microsoft Visual Studio Community 2017 修改新建项目的默认位置
IDE: Microsoft Visual Studio Community 2017 15.5.2 通过修改默认的设置,在下一次新建项目时,就可以节省一些不必要的操作. 菜单:工具 > 选项, ...
- HDU 2588 GCD(欧拉函数)
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 在th中显示图片
从DataTable中获取值: foreach (DataRow dr in ((DataTable)ViewBag.bookInfoList).Rows) { <tr> <th c ...
- cPanel中添加设置附加域(Addon domain)
本文介绍cPanel设置附加域(addon domain)来实现一个空间做多个网站的方法. 附加域(addon domain) 作用:通过它可以实现添加 新的顶级域名绑定到主机,从而创建新的站点.例如 ...
- 腾讯应用宝Android 应用加固(乐固)操作说明(转)
此处引用腾讯云对加固的优点说明如下: 为什么应用需要加固? 若应用不做任何安全防护,极易被病毒植入.广告替换.支付渠道篡改.钓鱼.信息劫持等,严重侵害开发者的利益. 应用进行安全防护,防止应用分发后, ...
- SVN不要显示问号
让SVN不要显示未进行版本控制的文件(夹)图标的问号: 1.选择TortoiseSVN→SettIngs 2.Overlays→取消勾选Unversioned,点击“应用”,然后重启电脑即可
- Idea的快捷键,瞎摸索,开心就好,哈哈哈
前言:如果你有强迫症,换了一个编辑器,最痛苦莫过于快捷键,不顺手了.这里自己瞎摸索的快捷键,贴一下,这里主要以实际应用为主,因为大量介绍的网上已经很多很多,So基本的不再叙述. 分享一下比较不错的Id ...
- oracle安装过程中先决条件检查失败的解决办法
1:公司数据库开始用的mysql,因为公司做的是保密性项目,所以就在项目日志过多的时候项目有爆炸的迹象啊(3000千万数据,貌似mysql有点撑不住).然后组长开始让我安装oracle,公司的内网也是 ...