PAT A1002 A+B for Polynomials

PAT A1002 A+B for Polynomials

题目描述:

  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 /****************************************************
 2 PAT A1002 A+B for Polynomials
 3 ****************************************************/
 4 #include <iostream>
 5 #include <iomanip>
 6 #include <cmath>
 7 #include <vector>
 8
 9 using namespace std;
10
11 struct term {
12     double exponent = 0;
13     double coefficient = 0;
14 };
15
16 int main() {
17     int K1 = 0, K2 = 0;
18     double tempExp = 0, tempCoe = 0;
19
20     cin >> K1;
21     vector<term> func1(K1);
22     for (int i = 0; i < K1; ++i) {
23         cin >> tempExp >> tempCoe;
24         func1[i].exponent = tempExp;
25         func1[i].coefficient = tempCoe;
26     }
27
28     cin >> K2;
29     vector<term> func2(K2);
30     for (int i = 0; i < K2; ++i) {
31         cin >> tempExp >> tempCoe;
32         func2[i].exponent = tempExp;
33         func2[i].coefficient = tempCoe;
34     }
35
36     vector<term> func3(K1 + K2);
37     int i = 0, j = 0, k = 0;
38     for (; i < func1.size() && j < func2.size(); ) {
39         if (func1[i].exponent == func2[j].exponent) {
40             func3[k].exponent = func1[i].exponent;
41             func3[k++].coefficient = func1[i++].coefficient + func2[j++].coefficient;
42         }
43         else if (func1[i].exponent > func2[j].exponent) {
44             func3[k].exponent = func1[i].exponent;
45             func3[k++].coefficient = func1[i++].coefficient;
46         }
47         else {
48             func3[k].exponent = func2[j].exponent;
49             func3[k++].coefficient = func2[j++].coefficient;
50         }
51
52         if (func3[k - 1].coefficient == 0 || fabs(func3[k - 1].coefficient) < 0.1) {
53             func3.erase(func3.begin() + k - 1);
54             --k;
55         }
56     }
57     func3.resize(k);
58     func3.insert(func3.end(), func1.begin() + i, func1.end());
59     func3.insert(func3.end(), func2.begin() + j, func2.end());
60
61     func3.size() == 0 ? cout << ‘0‘ : cout << func3.size() << ‘ ‘;
62     for (int i = 0; i < func3.size(); ++i) {
63         cout << setiosflags(ios::fixed) << setprecision(0) << func3[i].exponent << ‘ ‘;
64         cout << setiosflags(ios::fixed) << setprecision(1) << func3[i].coefficient;
65         if (i != func3.size() - 1) cout << ‘ ‘;
66     }
67
68     return 0;
69 }

注意事项:

  1:用空间换取时间也是一种减少代码运行时间的方法。

原文地址:https://www.cnblogs.com/mrdragon/p/11396041.html

时间: 2024-10-31 22:24:15

PAT A1002 A+B for Polynomials的相关文章

PAT甲级——A1009 Product of Polynomials

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?

[PTA] PAT(A) 1009 Product of Polynomials (25 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Result Problem portal:1009 Product of Polynomials Description Input Output Sample Sample Input Sample Output Solution Analysis Code #include <bits/stdc++.h>

PAT Advanced 1009 Product of 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 N?1?? a?N?1???? N?2?? a?N?2?

PAT 1002. A+B for Polynomials

#include<stdio.h> #include<string.h> double coefficient[1010]; int k1, k2,k3=0; int nk; int max_exponent1, max_exponent2; int min_exponent1, min_exponent2; int main(){ memset(coefficient, 0, sizeof coefficient); scanf("%d", &k1);

PAT:1009. Product of Polynomials (25) AC

#include<stdio.h> #include<stdlib.h> #include<string.h> //[warning]double 输入%lf,输出%f struct arr { int exp; //指数 double cof; //系数 }arr[1005]; double ans[2010]; //下标是指数,内容是系数 int main() { memset(arr,0,sizeof(arr)); memset(ans,0,sizeof(ans)

PAT/简单模拟习题集(二)

B1018. 锤子剪刀布 (20) Discription: 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. Input: 输入第1行给出正整数N(<=105),即双方交锋的次数.随后N行,每行给出一次交锋的信息,即甲.乙双方同时给出的的手势.C代表"锤子".J代表"剪刀".B代表"布",第1个字母代表甲方

PAT 1009

题意 模仿多项式相乘 思路 这道题与PAT A1002多项式的类型类似,也是用一个P[]存储指数到系数的映射关系,p[4]代表指数为4的项的系数,不过难点在于如何模拟乘法运算.首先先存储第一个多项式,也就是先存储第一次输入,而后在输入第二个多项式的时候,做边输入边处理的操作:每输入一个项的指数和系数,就枚举之前所有的p[],发现有p[i]!=0,说明存在项.即把指数与i相加,系数与p[i]相乘,并将得到的结果保存即可. 代码如下 #include <bits/stdc++.h> using n

PAT 1009. Product of Polynomials (25)

1009. Product of 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 polynomi

1009. Product of Polynomials (25)——PAT (Advanced Level) Practise

题目信息: 1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 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 case. Each c