PTA 1002 A+B for Polynomials
阅读原文时间:2023年07月10日阅读:3

问题描述:

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 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不应该是算有1项且N1和aN1均为0吗?!我考虑了这种情况,结果有一个结果迟迟不对,哪料到把这个删了就对了,天理难容!
代码是同学发给我改的,所以比较丑。同学代码写的比较臃肿,缩进也挺不舒服的,凑合着看吧。我改的时候都没搞成tab缩进,现在自然也懒得弄了,反正AC了。


代码:

#include
#include
#include
using namespace std;
int atotal[];
int ktotal;
float btotal[];

void mix(int na,int a[],int nb,int b[],float c[],float d[])
{
int ia=,ib=;
while(iab[ib])
{
atotal[ktotal]=a[ia];
btotal[ktotal]=c[ia];
ia++;
}
else if (a[ia]-0.05&&btotal[ktotal]<0.05)) ktotal++; } while(ia-0.05&&btotal[ktotal]<0.05)) ktotal++; ia++; } while(ib-0.05&&btotal[ktotal]<0.05)) ktotal++;
ib++;
}
}

int main()
{
int k1,k2;
int a1[],a2[];
float b1[],b2[];
cin>>k1;
for (int i=;i>a1[i];
cin>>b1[i];
}
cin>>k2;
for (int i=;i>a2[i];
cin>>b2[i];
}
mix(k1,a1,k2,a2,b1,b2);
cout<<ktotal;
cout.precision ();
cout.setf(ios::fixed | ios::showpoint );
for (int i=;i<ktotal;i++){
cout << " " << atotal[i] << " " << round(*btotal[i])/10.0;
}
return ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章