PAT 1002 A+B for Polynomials (25分)
题目
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
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≤N
K <⋯<N2 <N1 ≤1000.
Output Specification:
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
题目解读
给出两个多项式,每个输入格式是 非零项个数 指数1 系数1 指数2 系数2
让计算两多项式的和,并按照指定格式输出 非零项个数 指数1 系数1 指数2 系数2
要求顺序是指数从高到低。
思路解析
- 可以用一个
结构体
来保存每一项的指数和系数,然后在第二次输入时根据去找到相应的那一项,对其系数进行修改。 - 这样做既浪费存储空间也浪费时间,但一般都能想到,更好的做法是,用一个
数组
来取代整个结构体,每一项的指数作为数组的索引
,系数作为值
,这样在读入时,直接找到对应位置进行修改,对数组的访问是很快的。 - 之后一次遍历,统计出数组不为0的个数,就是
非零项的个数
;然后对数组从后往前输出每个非零项对应的下标和值,就是结果。
代码
#include <iostream>
using namespace std;
int main() {
// 指数作为下标,系数作为值,题目给出指数最多为1000
float pols[1001] = {0};
int k, exp;
float coe;
int cnt = 0;
// 每一个样例是两行
for (int i = 0; i < 2; ++i) {
// 第一个整数是说明后面有几个非0项
cin >> k;
for (; k > 0; --k) {
// 指数 系数 指数 系数
cin >> exp >> coe;
pols[exp] += coe;
}
}
// 统计非零项
for (int i = 0; i < 1001; ++i) {
if (pols[i] != 0) cnt++;
}
// 输出非零项个数
cout << cnt;
// 按 指数 系数,从高到底输出,空格分隔
for (int i = 1000; i >= 0; --i) {
if (pols[i] != 0) {
printf(" %d %.1f", i, pols[i]);
}
}
return 0;
}
PAT 1002 A+B for Polynomials (25分)的更多相关文章
- 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 1002. A+B for Polynomials (25) 简单模拟
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- PAT Advanced 1002 A+B for Polynomials (25 分)(隐藏条件,多项式的系数不能为0)
This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...
- 【PAT甲级】1002 A+B for Polynomials (25 分)
题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...
- PAT 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 con ...
- PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)
This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...
- 1002 A+B for Polynomials (25分)
This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...
- 1002 A+B for Polynomials (25分) 格式错误
算法笔记上能踩的坑都踩了. #include<iostream> using namespace std; float a[1001];//至少1000个位置 int main(){ in ...
- 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 ...
随机推荐
- 神奇的Kivy,让Python快速开发移动app
随着移动互联网的不断发展,手机.Pad等移动终端已经被普遍使用,充斥在人们的工作.学习和生活中,越来越多的程序都转向移动终端,各类app应用相拥而至. Kivy作为Python的Android和IOS ...
- Visual Studio 2015 + Windows 2012 R2, c++/cli Array::Sort() 抛出异常
在Windows7上编译就是正常. 可见Windows2012 R2缺少了一些东西. 另外,有一个现象一样,但原因不一样的 https://stackoverflow.com/questions/46 ...
- 设计模式 - 迭代器模式详解及其在ArrayList中的应用
基本介绍 迭代器模式(Iterator Pattern)是 Java 中使用最多的一种模式,它可以顺序的访问容器中的元素,但不需要知道容器的内部细节 模式结构 Iterator(抽象迭代器):定义遍历 ...
- Spring5参考指南:组件扫描
文章目录 组件扫描 @Component 元注解和组合注解 组件内部定义Bean元数据 为自动检测组件命名 为自动检测的组件提供作用域 生成候选组件的索引 组件扫描 上一篇文章我们讲到了annotat ...
- node.js koa 实现长轮询
长轮询的实现原理:浏览器发出请求之后,服务端资源如果没有就绪,那么并不立即返回,而是在一个时间范围内,不断地去查询资源是否就绪,如果就绪,就返回资源,如果超时了还没有就绪,就返回超时. 代码实现如下: ...
- 如何迅速分析出系统CPU的瓶颈在哪里?
内容出自极客时间专栏<Linux 性能优化实战> CPU 的性能指标那么多,CPU 性能分析工具一抓一大把,换成实际的工作场景,该观察什么指标.选择哪个性能工具呢? 不要担心,今天我就以多 ...
- Kudu,支持快速分析的新型Hadoop存储系统
Kudu是Cloudera开源的新型列式存储系统,是Apache Hadoop生态圈的新成员之一(incubating),专门为了对快速变化的数据进行快速的分析,填补了以往Hadoop存储层的空缺.本 ...
- Codeforce 1155D Beautiful Array(DP)
D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...
- 4)drf序列化组件 Serializer(偏底层)、ModelSerializer(重点)、ListModelSerializer(辅助群改)
知识点:Serializer(偏底层).ModelSerializer(重点).ListModelSerializer(辅助群改) 一.Serializer 偏底层 一般不用 理解原理 1.序列化准备 ...
- 快速幂 递归&&非递归 模板
一.递归版快速幂 inline int qpow(int x,int y,int p){ if(y==0) return 1; int z=qpow(x,y>>1,p); z=1ll*z* ...