首页 > 其他分享 >PAT 甲级 1002 A+B for Polynomials(25)

PAT 甲级 1002 A+B for Polynomials(25)

时间:2023-02-18 19:24:14浏览次数:36  
标签:25 PAT int scanf ++ num each 1002

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​​​ < ⋯ < 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

Experiential Summing-up

两种办法:

①map(较麻烦):使用三个 map ,键值对为<指数, 系数>,最后利用 vector 进行排序输出。

②array:使用一个数组,将指数相同的系数相加,统计非零系数的个数,最后倒序输出系数不为 0 的指数和系数。

Accepted Code

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int k, N;
 5 double a;
 6 vector<int> x;
 7 map<int, double> map1, map2, map3;
 8 
 9 int main()
10 {
11     cin >> k;
12     for(int i = 1; i <= k; i ++)
13     {
14         cin >> N >> a;
15         map1[N] = a;
16     }
17     cin >> k;
18     for(int i = 1; i <= k; i ++)
19     {
20         cin >> N >> a;
21         map2[N] = a;
22     }
23     for(int i = 0; i <= 1000; i ++)
24     {
25         if(map1[i] + map2[i] != 0)
26         {
27             x.push_back(i);
28             map3[i] = map1[i] + map2[i];
29         }
30     }
31 
32     sort(x.begin(), x.end());
33     reverse(x.begin(), x.end());
34 
35     cout << x.size();
36     for(int i = 0; i < x.size(); i ++)
37     {
38         cout << " " << x[i] << " ";
39         cout << fixed <<setprecision(1) <<map3[x[i]];
40     }
41     return 0;
42 }

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n, m, t;
 5 int cnt = 0;
 6 float num;
 7 float c[1001];
 8 
 9 int main()
10 {
11     scanf("%d", &n);
12     for(int i = 0; i < n; i ++)
13     {
14         scanf("%d%f", &t, &num);
15         c[t] += num;
16     }
17     scanf("%d", &m);
18     for(int i = 0; i < m; i ++)
19     {
20         scanf("%d%f", &t, &num);
21         c[t] += num;
22     }
23 
24     for(int i = 0; i < 1001; i ++)
25         if(c[i] != 0) cnt ++;
26     printf("%d", cnt);
27 
28     for(int i = 1000; i >= 0; i --)
29     {
30         if(c[i] != 0.0)
31             printf(" %d %.1f", i, c[i]);
32     }
33     return 0;
34 }

 

标签:25,PAT,int,scanf,++,num,each,1002
From: https://www.cnblogs.com/marswithme/p/17133330.html

相关文章

  • PAT 甲级 1001 A+B Format
    Calculate a+b andoutputthesuminstandardformat--thatis,thedigitsmustbeseparatedintogroupsofthreebycommas(unlesstherearelessthanfour......
  • PAT-basic-1020 月饼 java
    一、题目月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大......
  • PAT-basic-1016 部分A+B java
    一、题目正整数 A 的“DA​(为1位整数)部分”定义为由 A 中所有 DA​ 组成的新整数 PA​。例如:给定 A=3862767,DA​=6,则 A 的“6部分”PA​ 是66,因为 A 中......
  • PAT-basic-1018 锤子剪刀布 java
    一、题目大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大......
  • PAT-basic-1015 德才论 java c++
    一、题目宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而......
  • Geospatial,Hyperloglog,BitMap
    geospatial:地理位置底层实现geoadd:添加 geopos:获取指定的经纬度 geodist georadius:获取指定位置的指定半径的数据 georadiusbymember:获取指定元素指......
  • PAT-basic-1014 福尔摩斯的约会 java
    一、题目大侦探福尔摩斯接到一张奇怪的字条:我们约会吧!3485djDkxh4hhGE2984akDfkkkkggEdsbs&hgsfdkd&Hyscvnm大侦探很快就明白了,字条上奇怪的乱码实际上就是约......
  • PAT-basic-1012 数字分类 java
    一、题目给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:A1​ =能被5整除的数字中所有偶数的和;A2​ =将被5除后余1的数字按给出顺序进行交......
  • PAT-basic-1011 A+B 和 C java
    一、题目给定区间[−231,231]内的3个整数 A、B 和 C,请判断 A+B 是否大于 C。输入格式:输入第1行给出正整数 T (≤10),是测试用例的个数。随后给出 T 组......
  • PAT-basic-1010 一元多项式求导 java
    一、题目设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为nxn−1。)输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以......