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 N1 aN1 N2 aN2 ... NK aNKwhere 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.
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.
tips
注意0号测试点,系数为0时结果不输出,并且系数为0也不算在总个数里面测试点0未通过时,想到了是系数为0的问题,但是只是把coef != 0不输出,而忘了修改ans.size(),结果还是错误~因此找了不少时间 兜兜转转还是这个错误 只是没有考虑周到code
乘法 #include<iostream> #include <bits/stdc++.h> using namespace std; struct polynomials { int expo; //指数 double coef; //系数 polynomials(){} polynomials(int expo, double coef):expo(expo),coef(coef){} }a[11], b[11]; //要有默认构造函数 vector<polynomials> tmp, ans; bool cmp(polynomials x, polynomials y) { return x.expo > y.expo; } int main() { int k1, k2; scanf("%d",&k1); for(int i = 0; i < k1; i++){ scanf("%d%lf",&a[i].expo,&a[i].coef); } scanf("%d",&k2); for(int i = 0; i < k2; i++){ scanf("%d%lf",&b[i].expo,&b[i].coef); } for(int i = 0; i < k1; i++){ polynomials x = a[i]; for(int j = 0; j < k2; j++){ polynomials y = b[j]; tmp.push_back(polynomials(x.expo + y.expo, x.coef * y.coef)); } } sort(tmp.begin(), tmp.end(), cmp); int expo = tmp[0].expo; double coef = tmp[0].coef; for(int i = 1; i < tmp.size(); i++){ if(tmp[i].expo != tmp[i - 1].expo){ if(coef != 0) ans.push_back(polynomials(expo, coef)); expo = tmp[i].expo; coef = tmp[i].coef; } else { coef += tmp[i].coef; } } if(coef != 0) ans.push_back(polynomials(expo, coef)); printf("%d",ans.size()); for(int i = 0; i < ans.size(); i++){ printf(" %d %.1f",ans[i].expo, ans[i].coef); } return 0; }补充
加法 #include<iostream> #include <bits/stdc++.h> using namespace std; struct polynomials { int expo; //指数 double coef; //系数 polynomials(){} polynomials(int expo, double coef):expo(expo),coef(coef){} }a[11], b[11]; //要有默认构造函数 vector<polynomials> ans; int main() { int k1, k2; scanf("%d",&k1); for(int i = 0; i < k1; i++){ scanf("%d%lf",&a[i].expo,&a[i].coef); } scanf("%d",&k2); for(int i = 0; i < k2; i++){ scanf("%d%lf",&b[i].expo,&b[i].coef); } int i, j; for(i = 0, j = 0; i < k1 && j < k2; ) { polynomials x = a[i], y = b[j]; if(x.expo > y.expo){ ans.push_back(x); i++; } else if(x.expo < y.expo){ ans.push_back(y); j++; } else { //相等 ans.push_back(polynomials(x.expo, x.coef + y.coef)); i++; j++; } } while(i < k1){ ans.push_back(a[i]); i++; } while(j < k2){ ans.push_back(b[j]); j++; } printf("%d",ans.size()); for(int i = 0; i < ans.size(); i++){ printf(" %d %.1f",ans[i].expo,ans[i].coef); } return 0; }