您的位置:首页 > 其它

pat--1009product of polynomial解题报告

2018-03-24 19:47 267 查看
#include <cstdio>

///product of polynomial
float p1[1001], p2[1001], fin[2002];
float tp[1001][2002];

int main()
{
int k=0, k1=0, k2=0;
int index;
int maxIndex=0;

scanf("%d", &k);
k1 = k;

while( k-- )
{
scanf("%d", &index);
if( maxIndex<index )maxIndex = index;
scanf("%f", &p1[index]);
}
scanf("%d", &k);
k2 = k;
while( k-- )
{
scanf("%d", &index);
if( maxIndex<index )maxIndex = index;
scanf("%f", &p2[index]);
}
int sumIndex, tpIndex = 0;

for( int i=0;i<=maxIndex;i++ )
{
if( p2[i] )
{
for( int j=0;j<=maxIndex;j++ )
{
if( p1[j] )
{
tp[i][i+j] = (p2[i]*p1[j]);
if( tpIndex<i+j )tpIndex = i+j;
}
}
}
}
for( int i=0;i<=maxIndex;i++ )
{
for( int j=0;j<=tpIndex;j++ )
{
if( tp[i][j] )
{
fin[j] += tp[i][j];
}
}
}

for( int i=tpIndex;i>=0;i-- )
{
if( fin[i] )
{
sumIndex++;
}
}
printf("%d ", sumIndex);
for( int i=tpIndex;i>=0;i-- )
{
if( fin[i] )
{
sumIndex--;
printf("%d %.1f", i, fin[i]);
if( sumIndex )printf(" ");
}
}
return 0;
}
AC代码如上, 思路很简单,不缀述。
这里遇到一个问题, segment default,  起初以为数组越界, 浪费了很多时间。后来突然想到可能是数组开的过大的原因,查了下果然是这样的, 对, 栈炸了。具体见点击打开链接
解决方案有两种, 其一, 使用全局变量, 具体如上。其二,使用标准库stdlb.h下的malloc(free). 
路长且阻。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat