#include <stdio.h>
#include <math.h>
#include <string.h>
#define MAX 2000
double arr1[MAX],arr2[MAX],result[2*MAX];
int cnt=0,num1,num2;
void read(double a[],int & num){
int id;
double tmp;
scanf("%d",&num);
for (int i=0; i<num; i++) {
scanf("%d%lf",&id,&tmp);
a[id]=tmp;
}
}
void product(){
for (int i=0; i<=1000; i++) {
for (int j=0; j<=1000; j++) {
result[i+j]+=arr1[i]*arr2[j];
}
}
}
void output(){
for (int i=0; i<=2000; i++) {
if (result[i]!=0) {
cnt++;
}
}
if (cnt!=0) {
printf("%d",cnt);
for (int i=2000; i>=0; i--) {//一定不要忘记了=0
if (result[i]) {
printf(" %d %.1f",i,result[i]);
}
}
}
puts("");
}
int main(){
freopen("/Users/pantingting/Documents/code/data/input", "r", stdin);
read(arr1,num1);
read(arr2,num2);
product();
output();
return 0;
}
PAT 1009. Product of Polynomials (25)
最新推荐文章于 2015-03-13 23:07:29 发布