POJ 1555 Polynomial Showdown
2015-08-11 11:16
453 查看
Polynomial Showdown
Description
Given the coefficients of a polynomial from degree 8 down to 0, you are to format the polynomial in a readable format with unnecessary characters removed. For instance, given the coefficients 0, 0, 0, 1, 22, -333, 0, 1, and -1, you should generate an output
line which displays x^5 + 22x^4 - 333x^3 + x - 1.
The formatting rules which must be adhered to are as follows:
1. Terms must appear in decreasing order of degree.
2. Exponents should appear after a caret `"^".
3. The constant term appears as only the constant.
4. Only terms with nonzero coefficients should appear, unless all terms have zero coefficients in which case the constant term should appear.
5. The only spaces should be a single space on either side of the binary + and - operators.
6. If the leading term is positive then no sign should precede it; a negative leading term should be preceded by a minus sign, as in -7x^2 + 30x + 66.
7. Negated terms should appear as a subtracted unnegated term (with the exception of a negative leading term which should appear as described above). That is, rather than x^2 + -3x, the output should be x^2 - 3x.
8. The constants 1 and -1 should appear only as the constant term. That is, rather than -1x^3 + 1x^2 + 3x^1 - 1, the output should appear as-x^3 + x^2 + 3x - 1.
Input
The input will contain one or more lines of coefficients delimited by one or more spaces. There are nine coefficients per line, each coefficient being an integer with a magnitude of less than 1000.
Output
The output should contain the formatted polynomials, one per line.
Sample Input
Sample Output
Source
Mid-Central USA 1996
AC代码:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4494 | Accepted: 1554 |
Given the coefficients of a polynomial from degree 8 down to 0, you are to format the polynomial in a readable format with unnecessary characters removed. For instance, given the coefficients 0, 0, 0, 1, 22, -333, 0, 1, and -1, you should generate an output
line which displays x^5 + 22x^4 - 333x^3 + x - 1.
The formatting rules which must be adhered to are as follows:
1. Terms must appear in decreasing order of degree.
2. Exponents should appear after a caret `"^".
3. The constant term appears as only the constant.
4. Only terms with nonzero coefficients should appear, unless all terms have zero coefficients in which case the constant term should appear.
5. The only spaces should be a single space on either side of the binary + and - operators.
6. If the leading term is positive then no sign should precede it; a negative leading term should be preceded by a minus sign, as in -7x^2 + 30x + 66.
7. Negated terms should appear as a subtracted unnegated term (with the exception of a negative leading term which should appear as described above). That is, rather than x^2 + -3x, the output should be x^2 - 3x.
8. The constants 1 and -1 should appear only as the constant term. That is, rather than -1x^3 + 1x^2 + 3x^1 - 1, the output should appear as-x^3 + x^2 + 3x - 1.
Input
The input will contain one or more lines of coefficients delimited by one or more spaces. There are nine coefficients per line, each coefficient being an integer with a magnitude of less than 1000.
Output
The output should contain the formatted polynomials, one per line.
Sample Input
0 0 0 1 22 -333 0 1 -1 0 0 0 0 0 0 -55 5 0
Sample Output
x^5 + 22x^4 - 333x^3 + x - 1 -55x^2 + 5x
Source
Mid-Central USA 1996
AC代码:
#include <iostream> #include <cmath> using namespace std; int main(){ int polynomial[10]; int i,flag; while (cin>>polynomial[0]){ for(i=1;i<9;++i){ cin>>polynomial[i]; } flag=0; for(i=0;i<9;++i){ if(polynomial[i]){ if(flag)cout<<' '; if(flag)cout<<(polynomial[i]>0?"+ ":"- "); else if(polynomial[i]<0)cout<<"-"; if(abs(polynomial[i])!=1||i==8){ if(polynomial[i]<0)cout<<-polynomial[i]; else cout<<polynomial[i]; } if(i!=8){ cout<<"x"; if(i!=7)cout<<"^"<<8-i; } flag++; } } if(flag==0)cout<<"0"; cout<<'\12'; } return 0; }
相关文章推荐
- HDOJ--1233--还是畅通工程
- iOS创建调试证书真机调试
- 白话设计模式之--工厂方法和原型模式
- android安装
- javafx checkbox
- android scollview嵌套webview底部空白,高度无法自适应解决
- 浅谈共享软件如何不被暴力蹂躏
- [Pointer]使用指针时有可能导致非法内存操作
- CodeBlocks配置Qt 环境(含:预编译头文件,Qt设计师)
- 我花的钱都是父母挣的
- hdu 4109 拓扑排序 关键路径
- Ubuntu安装ssh,及失败解决方案
- fiddler stave插件
- Belkasoft Evidence Center could handle Chinese characters well
- python setup.py install 失败
- 解决有些Eclipse无法启动安装的Tomcat而是Eclipse自带的Tomcat
- 回车符,换行符与'\0'
- MySQL必知必会笔记(三)SELECT语句 联结表 高级联结 组合查询 全文本搜索
- ACdream 1073 雷霆战机
- android学习笔记NO.2