PAT A+B for Polynomials[简单]
1002 A+B for Polynomials (25)(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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
double nk1[]={},nk2[]={};
int main()
{
//freopen("1.txt","r",stdin);
int n;
scanf("%d",&n);
int a;
double b;
while(n--){
scanf("%d%lf",&a,&b);
nk1[a]=b;
}
scanf("%d",&n);
while(n--){
scanf("%d%lf",&a,&b);
nk2[a]=b;
} for(int i=;i<;i++){
nk1[i]=nk1[i]+nk2[i];
}
int ct=;
for(int i=;i<;i++){
if(nk1[i]!=){
ct++;
}
}
printf("%d",ct);
if(ct!=)printf(" ");
for(int i=;i>=;i--){
if(nk1[i]!=)
{printf("%d %.1f",i,nk1[i]);
ct--;
if(ct!=)printf(" ");
if(ct==)break;
}
} return ;
}
//写的代码有点烂。总之就是遍历呗,没想到很好的办法。就是这样下去。就是多项式对应系数相加。没什么难点。
发现了一个可以改进的地方,就是可以把第二个数组都进来的数直接相加,而不是定义两个数组。减小了空间占用。
PAT A+B for Polynomials[简单]的更多相关文章
- PAT(B) 1089 狼人杀-简单版(Java)逻辑推理
题目链接:1089 狼人杀-简单版 (20 point(s)) 题目描述 以下文字摘自<灵机一动·好玩的数学>:"狼人杀"游戏分为狼人.好人两大阵营.在一局" ...
- PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值
题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...
- PAT 1009 Product of Polynomials
1009 Product of Polynomials (25 分) This time, you are supposed to find A×B where A and B are two p ...
- PAT A1009 Product of Polynomials (25 分)——浮点,结构体数组
This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...
- PAT 1054 The Dominant Color[简单][运行超时的问题]
1054 The Dominant Color (20)(20 分) Behind the scenes in the computer's memory, color is always talke ...
- PAT 1019 General Palindromic Number[简单]
1019 General Palindromic Number (20)(20 分) A number that will be the same when it is written forward ...
- PAT World Cup Betting[非常简单]
1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over ...
- PAT Spell It Right [非常简单]
1005 Spell It Right (20)(20 分) Given a non-negative integer N, your task is to compute the sum of al ...
- PAT 1036 Boys vs Girls[简单]
1036 Boys vs Girls (25 分) This time you are asked to tell the difference between the lowest grade of ...
随机推荐
- 基于链表的C语言堆内存检测
说明 本文基于链表实现C语言堆内存的检测机制,可检测内存泄露.越界和重复释放等操作问题. 本文仅提供即视代码层面的检测机制,不考虑编译链接级的注入或钩子.此外,该机制暂未考虑并发保护. 相关性文章参见 ...
- 遍历json数组实现树
今天小颖在工作中遇到要遍历树得问题了,实现后,怕后期遇到又忘记啦,所以记录下嘻嘻,其实这个和小颖之前写过得一篇文章 json的那些事 中第4点有关json的面试题有些类似. 数组格式: v ...
- OpenStack cloud 第一天
这是刚接触openstack时候,看到的第一篇文章,感触很深,自己很喜欢的一个词Horizon就是出自本文 ============================================ ...
- minix中时间转换的实现(asctime.c)
在minix2.0源代码中,有相当经典的时间转换函数实现(src\ src\ lib\ ansi\ asctime.c),今天我们就来分析一下asctime.c中的源码 首先引入几个相关的头文件: 1 ...
- TypeScript中处理大数字(会丢失后面部分数字)
为啥要弄这玩意? 最近做数值游戏,需要用到很大的数字,在前端大数字会自动变成e的科学计数法. 有啥问题? 问题: 1. 在传递给服务端时,服务端因为不能处理大数字(怎么就处理不了?!),就想要我传字符 ...
- Unity3D如何有效地组织代码?(转)
问题: Unity3D可以说是高度的Component-Based Architecture,同时它的库提供了大量的全局变量.如何来组织代码呢? 答: - Unity有一些自身的约定,譬如项目里的Ed ...
- null类型
null类型只有一个特殊的值null null值表示一个空对象指针. var car = null; alert(typeof null);//object undefined派生自null ...
- Ubuntu 16.04系统下解决Vim乱码问题
方法: 打开终端输入:vim /etc/vim/vimrc,进入编辑模式,加入如下配置: set fileencodings=utf-8,gb2312,gbk,gb18030 set termenco ...
- Shell case
case 值 in模式1) command1 command2 command3 ;;模式2) command1 command2 command3 ;;*) command1 command2 co ...
- js获取文件对象