Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >PAT 1002 A+B for Polynomials (25分) 指数作为数组下标+系数作为值

PAT 1002 A+B for Polynomials (25分) 指数作为数组下标+系数作为值

作者头像
vivi
发布于 2020-07-14 02:40:25
发布于 2020-07-14 02:40:25
45000
代码可运行
举报
文章被收录于专栏:vblogvblog
运行总次数:0
代码可运行

题目

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 N​1​​ a​N​1​​ ​​ N​2​​ a​N​2​​ ​​ ... N​K​​ a​N​K​​ ​​ where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​ ​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N ​K​​ <⋯<N​2​​ <N​1​​ ≤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的个数,就是非零项的个数;然后对数组从后往前输出每个非零项对应的下标和值,就是结果。

代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-05-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
PAT(甲级)1002.A+B for Polynomials (25)
PAT 1002.A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynomials.
lexingsen
2022/02/25
3490
【PAT甲级】A+B for Polynomials
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
喜欢ctrl的cxk
2019/11/08
2820
【PAT甲级】1002 A+B for Polynomials (25分)
This time, you are supposed to find A+B where A and B are two polynomials.
韩旭051
2020/07/21
8400
PAT 1009 Product of Polynomials
1009. Product of 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 Specification: Each input file contains one test ca
ShenduCC
2018/04/26
8690
PAT (Advanced Level) Practice 1146 Topological Order (25分)
1146 Topological Order (25分) This is a problem given in the Graduate Entrance Exam in 2018: Which of
glm233
2020/09/28
4030
PAT (Advanced Level) Practice 1146 Topological Order (25分)
PAT 1025 PAT Ranking (25分) vector + sort
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.
vivi
2020/07/14
3550
PAT 1003 Emergency (25分) Dijstra
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
vivi
2020/07/14
3610
PAT 1024 Palindromic Number (25分) 使用string类reverse()
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
vivi
2020/07/14
2980
【PAT甲级】 A+B Format
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
喜欢ctrl的cxk
2019/11/08
3190
PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值
This time, you are supposed to find A×B where A and B are two polynomials.
vivi
2020/07/14
4430
PAT 1019 General Palindromic Number (20分)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.
vivi
2020/07/14
2430
PAT 1037 Magic Coupon (25分) 贪心+排序+负负/正正得正
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product... but hey, magically, they have some coupons with negative N's!
vivi
2020/07/14
4950
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分)
This time, you are supposed to find A+B where A and B are two polynomials.
glm233
2020/09/28
3040
PAT 1009 Product of Polynomials (25)
This time, you are supposed to find A*B where A and B are two polynomials.
week
2018/08/27
2660
PAT 1002 A+B for Polynomials
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 occu
ShenduCC
2018/04/27
6180
1002. A+B for Polynomials (25)
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.
week
2018/08/27
3780
PAT准备之2018.7.24
昨天被我划水滑过去了,今天终于完成了救赎,基本没有划水,一直在认真的学习,今天也做了不少题,发现自己还是有很多知识点薄弱的地方,还是基础不太好吧,以前总觉得自己这些东西都会,结果发现真到自己用的时候,真的是不会。。。 唉!这个暑假再把基础知识补一补吧。今天也是做了三道题。如下 1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, …, N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, …, N~j~ } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
全栈程序员站长
2022/07/25
1730
PAT 1048 Find Coins (25分) 硬币面值做数组下标即排序
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 105​​ coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.
vivi
2020/07/14
4470
PAT 1029 Median (25分) 有序数组合并
Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
vivi
2020/07/14
2760
PAT 1052 Linked List Sorting (25分) 结构体排序而已
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
vivi
2020/07/14
3520
推荐阅读
相关推荐
PAT(甲级)1002.A+B for Polynomials (25)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验