题目
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 (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤NK <⋯<N2 <N1 ≤1000.
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
我的解答
1
#include <iostream>
//果然还是忘了考虑0 的问题,如果系数为0,那么就不用输出了
//实际上我们知道,用一个数组的下标和值来存储这些数据是很方便的,但是可能我总觉得前面的数可能是小数
//也许应当转化为数学问题:
// 我需要实现多项式求和, 因此需要实现以下功能
// 1.相同项合并
// 2.为0项去除
// 3.以指数从大到小排列
using namespace std;
class pat_1002
{
public:
void merge_array(float *a,float *b,float *c)
{
int flag_a=1,flag_b=1,i=1,length_a=*a*2+1,length_b=*b*2+1;
while(flag_a<length_a&&flag_b<length_b)
{
if(*(a+flag_a)>*(b+flag_b))
{
*(c+i)=*(a+flag_a);
*(c+i+1)=*(a+flag_a+1);
i+=2;
flag_a+=2;
}
else if(*(a+flag_a)<*(b+flag_b))
{
*(c+i)=*(b+flag_b);
*(c+i+1)=*(b+flag_b+1);
i+=2;
flag_b+=2;
}
else if(*(a+flag_a)==*(b+flag_b))
{
*(c+i)=*(b+flag_b);
*(c+i+1)=*(b+flag_b+1)+*(a+flag_a+1);
flag_b+=2;
flag_a+=2;
}
if(c[i+1]!=0)i+=2;
}
while(flag_a<length_a)
{
*(c+i)=*(a+flag_a);
*(c+i+1)=*(a+flag_a+1);
i+=2;
flag_a+=2;
}
while(flag_b<length_b)
{
*(c+i)=*(b+flag_b);
*(c+i+1)=*(b+flag_b+1);
i+=2;
flag_b+=2;
}
*c=i/2;
cout<<*c;
for(int j=1;j<i;j++)cout<<" "<<*(c+j);
}
};
int main()
{
int a,b;
float array_1[2002]={0},array_2[2002]={0},array_3[2002]={0};
cin>>a;
array_1[0]=a;
for(int i=1;i<=a*2;i++)cin>>array_1[i];
cin>>b;
array_2[0]=b;
for(int i=1;i<=b*2;i++)cin>>array_2[i];
pat_1002 pat;
pat.merge_array(array_1,array_2,array_3);
return 0;
}
真的不想改了,实在太累了
终于知道为什么了,原来是最后输出格式有问题,改成下面的就好
cout<<*c;
for(int j=1;j<i;j+=2)printf(" %.0f %.1f",c[j],c[j+1]);
实在太沙雕了,可能是因为系数没有留小数吧,在他们为整数的时候。
2
#include<iostream>
#include<stdio.h>
using namespace std;
class pat_1002
{
public:
int lenth;
float temp;
void sort_arr(float *a)
{
for(int i=1;i<=lenth;i+=2)
{
for(int j=1;j<=lenth-2;j+=2)
{
if(*(a+j)<*(a+j+2))
{
temp=*(a+j);
*(a+j)=*(a+j+2);
*(a+j+2)=temp;
temp=*(a+j+1);
*(a+j+1)=*(a+j+3);
*(a+j+3)=temp;
}
}
}
}
};
int main()
{
float arrey[2001];
float brrey[2001];
int lenth_a,lenth_b;
int i=0;
while(scanf("%f",&arrey[i])!=0)
{
i++;
if(getchar()==10)break;
}
lenth_a=i;
i=0;
while(scanf("%f",&brrey[i])!=0)
{
i++;
if(getchar()==10)break;
}
lenth_b=i;
int flag_b=lenth_b;
int flag_a=1;
for(i=1;i<lenth_b;)
{
if(brrey[i]==arrey[flag_a])
{
brrey[i+1]+=arrey[flag_a+1];
flag_a+=2;
i+=2;
}
else if(brrey[i]<arrey[flag_a])
{
brrey[flag_b]=arrey[flag_a];
brrey[flag_b+1]=arrey[flag_a+1];
flag_b+=2;
flag_a+=2;
}
else i+=2;
if(flag_a>=lenth_a)break;
}
if(flag_a<lenth_a)
{
for(i=flag_a;i<lenth_a;i+=2)
{
brrey[flag_b]=arrey[i];
brrey[flag_b+1]=arrey[i+1];
flag_b+=2;
}
}
brrey[0]=flag_b/2;
pat_1002 pat;
pat.lenth=flag_b;
pat.sort_arr(brrey);
cout<<brrey[0];
for(i=1;i<flag_b;i++)
cout<<" "<<brrey[i];
return 0;
}
备注:这两个代码均不能完全通过测试。
看了别人的
一个感觉蛮完美的
https://blog.csdn.net/zcmuczx/article/details/53953916
用这种方法求多项式和,几乎完美吧。