问题描述:
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 aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (,) are the exponents and coefficients, respectively. It is given that 1,0.
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.5Sample Output:
3 2 1.5 1 2.9 0 3.2
这题测试数据魔鬼!魔鬼!魔鬼!同志们,作为多项式看待的时候,常数0不应该是算有1项且N1和aN1均为0吗?!我考虑了这种情况,结果有一个结果迟迟不对,哪料到把这个删了就对了,天理难容!代码是同学发给我改的,所以比较丑。同学代码写的比较臃肿,缩进也挺不舒服的,凑合着看吧。我改的时候都没搞成tab缩进,现在自然也懒得弄了,反正AC了。
代码:
1 #include<iostream> 2 #include<iomanip> 3 #include<cmath> 4 using namespace std; 5 int atotal[100]; 6 int ktotal; 7 float btotal[100]; 8 9 void mix(int na,int a[],int nb,int b[],float c[],float d[]) 10 { 11 int ia=0,ib=0; 12 while(ia<na&&ib<nb) 13 { 14 if (a[ia]>b[ib]) 15 { 16 atotal[ktotal]=a[ia]; 17 btotal[ktotal]=c[ia]; 18 ia++; 19 } 20 else if (a[ia]<b[ib]) 21 { 22 atotal[ktotal]=b[ib]; 23 btotal[ktotal]=d[ib]; 24 ib++; 25 } 26 else 27 { 28 atotal[ktotal]=a[ia]; 29 btotal[ktotal]=c[ia]+d[ib]; 30 ia++;ib++; 31 } 32 if (!(btotal[ktotal]>-0.05&&btotal[ktotal]<0.05)) ktotal++; 33 } 34 while(ia<na) 35 { 36 atotal[ktotal]=a[ia]; 37 btotal[ktotal]=c[ia]; 38 if (!(btotal[ktotal]>-0.05&&btotal[ktotal]<0.05)) ktotal++; 39 ia++; 40 } 41 while(ib<nb) 42 { 43 atotal[ktotal]=b[ib]; 44 btotal[ktotal]=d[ib]; 45 if (!(btotal[ktotal]>-0.05&&btotal[ktotal]<0.05)) ktotal++; 46 ib++; 47 } 48 } 49 50 int main() 51 { 52 int k1,k2; 53 int a1[100],a2[100]; 54 float b1[100],b2[100]; 55 cin>>k1; 56 for (int i=0;i<k1;i++){ 57 cin>>a1[i]; 58 cin>>b1[i]; 59 } 60 cin>>k2; 61 for (int i=0;i<k2;i++) 62 { 63 cin>>a2[i]; 64 cin>>b2[i]; 65 } 66 mix(k1,a1,k2,a2,b1,b2); 67 cout<<ktotal; 68 cout.precision (1); 69 cout.setf(ios::fixed | ios::showpoint ); 70 for (int i=0;i<ktotal;i++){ 71 cout << " " << atotal[i] << " " << round(10*btotal[i])/10.0; 72 } 73 return 0; 74 }
来源:https://www.cnblogs.com/jarvis-yang/p/12288750.html