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 ...
随机推荐
- vuex报错: [vuex] Expects string as the type, but found undefined.
报错如图 检查了好久,发现 import * as types from '../mutation-types'const actions = { add({commit}){ commit(type ...
- 部署OpenStack问题汇总(四)--openstack中nova-compute状态status显示为'XXX'的问题
本博客已经添加"打赏"功能,"打赏"位置位于右边栏红色框中,感谢您赞助的咖啡. 第一次部署openstack的时候就遇见了这个问题,当时的版本是havana, ...
- zabbix配置server,proxy,agent架构
author: headsen chen date:2018-10-30 19:49:50 环境: centos 6.8_x86_64 zabbix-server: 192.168.1.130 z ...
- docker swarn集群笔记
.安装Docker 三剑客: curl -L https://github.com/docker/machine/releases/download/v0.10.0/docker-machine-`u ...
- 关于 Java 泛型的一些有趣的例子
有以下的代码: try { ArrayList<String> lstA = new ArrayList<String>(); ArrayList<Integer> ...
- 机器学习TensorFlow安装经过摘要
第一步:我在Github上面下载了TensorFlow项目源码 第二步:在tensorflow-master/tensorflow/docs_src/install里面找到了install_mac.m ...
- Cache replacement policies 缓存实现算法
Cache replacement policies - Wikipedia https://en.wikipedia.org/wiki/Cache_replacement_policies Cach ...
- Unified Modeling Language
https://en.wikipedia.org/wiki/Unified_Modeling_Language
- Networking---poj1287最小生成树
http://poj.org/problem?id=1287 最小生成树模板题类似的还有:poj1258 hdu1233代码几乎一样: 最小生成树详解 #include<stdio.h> ...
- Python开发【笔记】:加锁的最佳方案
避开死锁 代码程序中,尽量要避免死锁的产生,下面分析常见的线程锁使用方式 :注:只有同一把锁才会产生互斥 1.常见的死锁方式(加锁时程序报错,锁未释放): import time import thr ...