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 ...
随机推荐
- C# 基础知识系列- 13 常见类库介绍(二)日期时间类
0. 前言 上一篇内容介绍了Console类和Math类,这篇内容着重介绍一下C#中时间日期的处理方式. 上一篇勘误: 上一篇中关于静态类没有构造函数,这一表述有误.正确的说法是C#中静态类不包含常规 ...
- pytorch中的前项计算和反向传播
前项计算1 import torch # (3*(x+2)^2)/4 #grad_fn 保留计算的过程 x = torch.ones([2,2],requires_grad=True) print(x ...
- pytorch中tensor的属性 类型转换 形状变换 转置 最大值
import torch import numpy as np a = torch.tensor([[[1]]]) #只有一个数据的时候,获取其数值 print(a.item()) #tensor转化 ...
- JDBC 中的事务和批处理 batch
JDBC事务处理: 事务处理一般在事务开始前把事务提交设置为false 所有DML语句执行完成后提交事务 demo: package com.xzlf.jdbc; import java.sql.Co ...
- Docker 中如何安装配置 Nginx
拉取 nginx 最新版镜像,然后简单启动一个 nginx 容器: docker pull nginx:latest docker run --name nginx01 -d -p 80:80 ngi ...
- 调用ocx ActiveX控件详解(做一个简单的ocx控件)
背景 最近做的项目都和插件有关,就是在页面中调用插件的方法,然后进行操作. 插件就是ocx ActiveX控件,具体的说明可以自己去了解一下,在这里就不做赘述. 具体调用方式很简单: 1.在页面中写一 ...
- 2019-2020-1 20199308《Linux内核原理与分析》第二周作业
<Linux内核分析> 第一章 计算机工作原理 1.1 存储程序计算机工作模型 冯·诺依曼体系结构 各种计算机体系结构需要遵从的一个"客观规律" 结构图 冯·诺依曼体系 ...
- .net多线程归并排序
一.概述 在了解排序算法的同时,想到用多线程排序减少排序的时间,所以写了一个简单的示例,加深印象.下面是具体代码 二.内容 环境:vs2017,.net core 2.2 控制台程序. 运行时使用r ...
- 【Linux常见命令】tee命令
tee - read from standard input and write to standard output and files tee命令用于读取标准输入的数据,并将其内容输出成文件. t ...
- Elasticsearch: 权威指南 » 深入搜索 » 多字段搜索 » 多数字段 good
跨字段实体搜索 » 多数字段编辑 全文搜索被称作是 召回率(Recall) 与 精确率(Precision) 的战场: 召回率 --返回所有的相关文档:精确率 --不返回无关文档.目的是在结果的 ...