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 1001 A+B 解题报告
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 解题报告
- PAT 解题报告 1009. Product of Polynomials (25)
- PAT (Advanced Level) 1101. Quick Sort (25) 解题报告
- PAT 银行排队问题之单窗口“夹塞”版 (队列+模拟) -- 解题报告
- PAT (Basic Level) Practise (中文)1067. 试密码(20) 解题报告
- PAT (Advanced Level) 1008. Elevator (20) 解题报告
- PAT 1025 反转链表 (25) 解题报告
- PAT 解题报告 1010. Radix (25)
- PAT 航空公司VIP客户查询 (哈希 | map) -- 解题报告
- 解题报告-PAT-Pop Sequence 浙大1051
- PAT 解题报告 1047. Student List for Course (25)
- PAT解题报告索引
- 【解题报告】【浙大PAT】03-树1. List Leaves (25)
- PAT (Advanced Level) 1102. Invert a Binary Tree (25) 解题报告
- pat(basic level) practise 解题报告
- 解题报告-PAT-Complete Binary Search Tree
- 1093. Count PAT's (25)解题报告
- Java 实现PAT乙级(Basic Level)1001-1010 解题报告(一)
- PAT 解题报告 1048. Find Coins (25)