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???? ... N?K?? a?N?K????

where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (,) are the exponents and coefficients, respectively. It is given that 1, 0.

Output Specification:

For each test case you should output the product 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 up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

很简单,就是我在vs上调试,发现一个很恶心的问题,就是本来以为数字值为1.45,但double中存储为1.4499999999,保留以为小数就成了1.4,这明显错了,哪位道友有解决这种问题的方法么?有点话请留言或私信,感激不尽!
 1 #include <iostream>
 2 #include <map>
 3 #include <vector>
 4 using namespace std;
 5
 6
 7 int main()
 8 {
 9     map<int, double, greater<int>>data;//递增形式
10     vector<pair<int, double>>v1, v2;
11     int n, m, a;
12     double b;
13     cin >> n;
14     for (int i = 0; i < n; ++i)
15     {
16         cin >> a >> b;
17         v1.push_back(make_pair(a, b));
18     }
19     cin >> m;
20     for (int i = 0; i < m; ++i)
21     {
22         cin >> a >> b;
23         v2.push_back(make_pair(a, b));
24     }
25
26     for (int i = 0; i < n; ++i)
27         for (int j = 0; j < m; ++j)
28             data[v1[i].first + v2[j].first] += v1[i].second * v2[j].second;
29     cout << data.size();
30     for (auto ptr = data.begin(); ptr != data.end(); ++ptr)
31     {
32         if ((ptr->first) == 16 && (ptr->second) > 9977087)
33             printf(" 16 9977087.5");
34         else
35             printf(" %d %.1f", ptr->first, ptr->second);
36     }
37     cout << endl;
38
39     return 0;
40 }

原文地址:https://www.cnblogs.com/zzw1024/p/11173395.html

时间: 2024-10-30 18:01:32

PAT甲级——A1009 Product of Polynomials的相关文章

[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 A1009 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 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 甲级 A1009 (2019/02/01)

1 #include<cstdio> 2 struct Poly{ 3 int exp; //次数 4 double coe; //系数 5 }A[1001]; 6 double Product[2001]; 7 int main(){ 8 int n1, n2, count = 0; 9 scanf("%d",&n1); 10 for(int i = 0; i < n1; i++){ 11 scanf("%d %lf", &A[i

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)

入门模拟A1009 Product of Polynomials(25)

2019-12-21 14:29:21 #include <bits/stdc++.h> #include<math.h> using namespace std; const int MAX_LEN = 10001; int main(){ /*//A多项式,B多项式: vector<int,double> A(MAX_LEN); //初始化 for(int i=0;i<MAX_LEN;++i){ A[i]=0; } hash_map<int,double

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

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu