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 coeficients(系數), respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=1000.
Output Specification:
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.
Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 3 3.6 2 6.0 1 1.6
題目意思:給你兩個多項式A和B,求A*B的結果。
解題思路:兩個多項式相乘,系數coeficients相乘,指數exponents相加,模拟一下即可。這裡由于指數是連續的,系數不連續且是小數,是以可以用數組來儲存多項式,指數作為數組下标,系數儲存到數組中。最後按照指數遞減的順序輸出所有的不為0的項即可。
decimal
adj. 小數的;十進位的
n. 小數
Product
n. 乘積、産物
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
int main()
{
int n1,n2,cnt=0;
int i,j,e;
double c;
double a[2010]={0.0},ans[2010]={0.0};
scanf("%d",&n1);
for(i=0;i<n1;i++)
{
scanf("%d %lf",&e,&c);
a[e]=c;
}
scanf("%d",&n2);
for(i=0;i<n2;i++)
{
scanf("%d %lf",&e,&c);
for(j=0;j<1010;j++)
{
if(a[j]!=0)
{
if(a[j]*c>10.0)
{
ans[j+e+1]+=(a[j]*c)/10.0;//産生進位
}
else
{
ans[j+e]+=a[j]*c;//系數相乘,指數相加
}
}
}
}
for(i=0;i<2010;i++)
{
if(ans[i]!=0)
{
cnt++;
}
}
printf("%d",cnt);//所有不為0的項數數量
for(i=2010-1;i>=0;i--)
{
if(ans[i]!=0)
{
printf(" %d %.1f",i,ans[i]);
}
}
return 0;
}
作者:王陸