1009. Product of Polynomials (25)
2015-11-21 21:01
316 查看
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 coefficients, 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
Sample Output
3 3 3.6 2 6.0 1 1.6
-----------------------华丽的分割线-------------------------
分析:感觉输入量不是很大,就用数组来表示多项式以及存结果了。
代码:
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 coefficients, 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
-----------------------华丽的分割线-------------------------
分析:感觉输入量不是很大,就用数组来表示多项式以及存结果了。
代码:
#include<cstdio> #include<cstdlib> #define OutputMaxn 2001 #define InputMaxn 1001 double input[2][InputMaxn]; double result[OutputMaxn]; int main(void) { int K,i,j,exp; double coef; for(j=0;j<2;++j) { scanf("%d",&K); for(i=0;i<K;++i) { scanf("%d %lf",&exp,&coef); input[j][exp] = coef; } } for(i=0;i<InputMaxn;++i) for(j=0;j<InputMaxn;++j) { result[i+j] += input[0][i] * input[1][j]; } int count = 0; for(i=0;i<OutputMaxn;++i) { if(result[i] != 0.0) ++count; } printf("%d ",count); if(count > 0) { for(i=OutputMaxn - 1;i>=0;--i) { if(result[i] != 0.0) { printf("%d %.1f",i,result[i]); --count; if(count!=0) printf(" "); } } } else { printf("0 0.0"); } system("pause"); return 0; }
相关文章推荐
- json数据的解析
- 仿QQ5.0侧滑菜单—进阶篇
- Unity中的输入
- 函数与优化
- 数据库锁(一)
- Java中this分类以及在各分类下的用法或规则
- poj-1852
- SSL工作原理
- js对象形式编码风格
- 116 老生常谈session,cookie的区别,安全性
- iOS如何实现表格的折叠效果?
- JavaScript学习之路01_准备
- svn使用发现新问题.
- “应试教育的死穴,恰在于堵住了孩子“犯错”空间”——有感
- 关于Popupwindow的一些理解
- A. Patrick and Shopping
- LeetCode 055 Jump Game
- Android TextView 判断文字内容是否超出显示省略号
- Postfix之sasldb2
- 虚拟机网络连接方式