1009. Product of Polynomials (25)
题目:
This time, you are supposed to find A*B where A and B are two polynomials.
输入格式:
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.
输出格式:
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.
输入样例:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出样例:
3 3 3.6 2 6.0 1 1.6
思路:
先得到第一个多项式系数和指数
接着输入第二个多项式,输入的时候将第二个多项式的指数与第一个多项式的每一个指数相加,对应系数相乘,核心代码如下:
ans[exp + poly[j].exp] += cof*poly[j].cof; //与第一个多项式每一项相乘
这里要用一个struct来表示第一个多项式,可以用数组(简单好写),也可以用链表来存(省空间)。下面的version1.0使用链表存的,version2.0是用数组来存的。
代码:
version1.0(链表存储):
/*
* @tag PAT_A_1009
* @authors R11happy (xushuai100@126.com)
* @date 2016-07-20 20:08:15-21:04
* @version 1.0
* @Language C++
* @Ranking 130/4240
*/
#include <cstdio>
#include <cstdlib> //用malloc要用<cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
using namespace std;
typedef struct Poly* PtrtoPoly;
struct Poly
{
int expo;
double coef;
PtrtoPoly next;
};
double res[2005]; //res[i]用来存指数为i的系数
int main()
{
int k;
int expo;
double coef;
stack<int> st;
int Max = 0; //记录出现过的最大指数
int cnt = 0; //记录输出的多项式项数
PtrtoPoly a = (PtrtoPoly)malloc(sizeof(struct Poly));
a->next = NULL;
scanf("%d", &k);
while (k--) //输入第一个多项式,用链表存储
{
PtrtoPoly tmpa = (PtrtoPoly)malloc(sizeof(struct Poly));
scanf("%d", &(tmpa->expo));
if (tmpa->expo > Max)
Max = tmpa->expo;
scanf("%lf", &(tmpa->coef));
tmpa->next = a->next;
a->next = tmpa;
}
scanf("%d", &k);
while (k--) //输入第二个多项式
{
PtrtoPoly tmpb = (PtrtoPoly)malloc(sizeof(struct Poly));
scanf("%d", &expo);
int index = expo;
scanf("%lf", &coef);
tmpb = a->next;
while (tmpb)
{
index = expo + tmpb->expo;
if (index > Max)
Max = index;
res[index] += (coef * tmpb->coef);
tmpb = tmpb->next;
}
}
for (int i = 0; i <= Max; i++)
{
if (res[i])
{
st.push(i); //对所有非零项,push指数
cnt++;
}
}
printf("%d", cnt); //最初为printf("%d ", cnt); 但当只输出0时末尾有多余空格
for (int i = 0; i<cnt; i++)
{
int index = st.top(); //记录栈顶元素,即结果项的指数
printf(" %d %.1f", index, res[index]);
st.pop(); //堆栈的使用要记得pop栈顶
}
printf("\n");
return 0;
}
version2.0(数组存储)
/*
* @tag PAT_A_1009
* @authors R11happy (xushuai100@126.com)
* @date 2016-07-20 23:53:15-24:19
* @version 2.0
* @Language C++
* @Ranking 130/4240
*/
#include <cstdio>
struct Poly
{
int exp; //指数
double cof; //系数
}poly[1001]; //第一个多项式
double ans[2001]; //存放结果
int main()
{
int n, m, cnt = 0;
scanf("%d", &n); //第一个多项式中非零系数的项数
for (int i = 0; i < n; i++)
scanf("%d%lf", &poly[i].exp, &poly[i].cof);
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
int exp;
double cof;
scanf("%d%lf", &exp, &cof);
for (int j = 0; j < n; j++)
{
ans[exp + poly[j].exp] += cof*poly[j].cof; //与第一个多项式每一项相乘
}
}
for (int i = 0; i <= 2000; i++) //统计非零项数
{
if (ans[i])
cnt++;
}
printf("%d", cnt);
for (int i = 2000; i >= 0; i--) //遍历整个数组,找非零项输出
{
if (ans[i] != 0.0)
{
printf(" %d %.1f", i, ans[i]);
}
}
return 0;
}
收获:
- 链表存储要先干三件事:
1)typedef struct Poly* PtrtoPoly;
2)struct Poly
{
int expo;
double coef;
PtrtoPoly next;
};
3)PtrtoPoly a = (PtrtoPoly)malloc(sizeof(struct Poly));
a->next = NULL; - 追求简单的话建议直接采用暴力求解,开俩数组进行操作
- 用malloc要用
- 用stack要用using namespace std;
- 答案的系数数组最少要开到2001