PAT甲级1002题解
2017-03-18 21:44
369 查看
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 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.
Output
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 注:此题应注意和系数为0的项不符合题意,需要忽略。且输入数据已经按exponents降序排列,无需使用sort进行排列。 代码如下所示: #include<stdio.h> #include<stdlib.h> #include<algorithm> #include<iostream> using namespace std; struct stru { int exponents; float coefficients; }; stru stru1[1000], stru2[1000];//分别存储第一行和第二行多项式 int main() { int K1, K2; while (scanf("%d", &K1) != EOF) { int i = 0; for (i = 0; i < K1; i++) { scanf("%d%f", &stru1[i].exponents, &stru1[i].coefficients); } scanf("%d", &K2); for (i = 0; i < K2; i++) { scanf("%d%f", &stru2[i].exponents, &stru2[i].coefficients); } stru stru3[1000];//存储结果的多项式 int m = 0, n = 0,k=0; while (m < K1&&n < K2) { if (stru1[m].exponents == stru2 .exponents) { if (stru1[m].coefficients + stru2 .coefficients != 0) { stru3[k].coefficients = stru1[m].coefficients + stru2 .coefficients; stru3[k].exponents = stru1[m].exponents; k++; m++; n++; } else { m++; n++; } } else if (stru1[m].exponents < stru2 .exponents) { stru3[k].exponents = stru2 .exponents; stru3[k].coefficients = stru2 .coefficients; n++; k++; } else { stru3[k].exponents = stru1[m].exponents; stru3[k].coefficients = stru1[m].coefficients; m++; k++; } } while (m < K1) { stru3[k].exponents = stru1[m].exponents; stru3[k].coefficients = stru1[m].coefficients; k++; m++; } while (n < K2) { stru3[k].exponents = stru2 .exponents; stru3[k].coefficients = stru2 .coefficients; k++; n++; } printf("%d", k); for (i = 0; i < k; i++) { printf(" %d %.1f", stru3[i].exponents, stru3[i].coefficients); } printf("\n"); } return 0; }
相关文章推荐
- PAT 甲级 1002
- PAT 甲级1002 A+B for Polynomials (25)
- PAT程序设计练习——甲级1002(两个多项式的解析与合并)
- PAT甲级C语言.1002. 写出这个数
- PAT甲级1002
- pat甲级1002
- PAT 甲级1002_A+B for Polynomials (25)
- PAT 甲级 1002
- PAT甲级1002
- PAT甲级1002 A+B for Polynomials
- PAT甲级1002
- PAT甲级 1002 A+B for Polynomials (25)
- PAT程序设计考题——甲级1002(A+B for Polynomials ) C++实现
- 【PAT】甲级1002 - A+B for Polynomials(多项式加法)
- 浙大pat | 浙大pat 牛客网甲级 1002 All Roads Lead to Rome (30)迪杰斯特拉算法改进
- PAT甲级1002 A + B
- PAT甲级.1002. A+B for Polynomials (25)
- PAT-甲级1002
- PAT甲级1002
- PAT甲级1002