您的位置:首页 > 其它

1002. A+B for Polynomials (25)

2018-03-20 13:46 330 查看
This time, you are supposed to find A+B where A and B are two polynomials.InputEach 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.OutputFor 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 Output3 2 1.5 1 2.9 0 3.2

思路

条件:①1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.②输入格式分别为 N(几个非零多项式) NK(指数) NA(系数)
题目读懂,再看看例子大概就明白想要的效果是什么样的。
题目中:
A=2.4^1+3.2^1;
B=1.5^2+0.5^1;
A+B=1.5^2+2.9^1+3.2^0=1.5^2+(2.4+0.5)^1+3.2^0;
没有要求具体算出值,但对于指数相同的系数要合并,同时系数要按非递增的顺序排列

C++:#include<cstdio>
const int max_n =1111;

int main(){
float k[max_n]={};
float a;
int ka,n,count=0;
//输入第一行
scanf("%d",&ka);
for(int i=0;i<ka;i++){
scanf("%d %f",&n,&a);
k
+=a;
}
//输入第二行
scanf("%d",&ka);
for(int i=0;i<ka;i++){
scanf("%d %f",&n,&a);
k
+=a;
}
for(int i=0;i<max_n;i++)
if(k[i]!=0)count++;

printf("%d",count);
for(int i=max_n-1;i>=0;i--)
if(k[i]!=0)
printf(" %d %.1f",i,k[i]);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: