【寒假任务】洛谷1067 多项式输出
来源:互联网 发布:淘宝保证金别骗怎么办 编辑:程序博客网 时间:2024/04/28 17:46
问题描述
给出多项式的最高次项,再给出各次项的系数,要求将此多项式补全。
样例输入
5
100 -1 1 -3 0 10
样例输出
100x^5-x^4+x^3-3x^2+10
算法讨论
这道题不难,纯粹的模拟。但是它是一个巨坑!巨坑!!首先需要注意的是系数的绝对值为1时,不能输出1x,要输出x,还有就是指数为1时只能输出x,不能输出x^1,还有种情况就是当系数为负数时不要多加一个负号。基本要注意的就这些。时间复杂度O(n)。
const maxn=101;var a:array[1..maxn] of longint; i,j,n,m:longint; st,st1,st2:string;begin read(n); for i:=1 to n+1 do read(a[i]); if n=0 then halt; m:=n; str(a[1],st1); str(m,st2); if a[1]=1 then st:='x^'+st2 else if a[1]=-1 then st:='-x^'+st2 else st:=st1+'x^'+st2; dec(m); for i:=2 to n do begin if a[i]>0 then if a[i]<>1 then begin str(a[i],st1); str(m,st2); if m=1 then st:=st+'+'+st1+'x' else st:=st+'+'+st1+'x^'+st2; dec(m) end else begin str(m,st2); if m=1 then st:=st+'+x' else st:=st+'+x^'+st2; dec(m) end; if a[i]<0 then if a[i]<>-1 then begin str(a[i],st1); str(m,st2); if m=1 then st:=st+st1+'x' else st:=st+st1+'x^'+st2; dec(m) end else begin str(m,st2); if m=1 then st:=st+'-x' else st:=st+'-x^'+st2; dec(m) end; if a[i]=0 then dec(m) end; str(a[n+1],st1); if a[n+1]>0 then st:=st+'+'+st1; if a[n+1]<0 then st:=st+st1; writeln(st)end.
Pixiv ID:60209427
0 0
- 【寒假任务】洛谷1067 多项式输出
- 洛谷1067 多项式输出
- [洛谷1067]多项式输出
- 【寒假任务】洛谷1540 机器翻译
- 【寒假任务】洛谷1125 笨小猴
- 【寒假任务】 洛谷1093 奖学金
- 洛谷 P1067 多项式输出
- 洛谷 P1067 多项式输出
- 洛谷 P1067 多项式输出
- 洛谷 P1067 多项式输出
- 寒假任务
- 【模拟】洛谷 P1067 多项式输出
- 洛谷P1067多项式输出 题解
- 【洛谷】P1067 多项式输出【模拟】
- 【寒假任务】洛谷1003 铺地毯
- 【寒假任务】 洛谷1177 快速排序
- 【寒假任务】 洛谷1059 明明的随机数
- 【寒假任务】 洛谷1068 分数线划定
- 中断调试小结
- 字符串求最长公共子序列(相似度计算)
- 悬链线
- 计数排序
- javaweb第一课,Tomcat的安装和配置
- 【寒假任务】洛谷1067 多项式输出
- leetcode141&234
- 如何对n个大小都小于100的整数进行排序,要求时间复杂度O(n),空间复杂度O(1)。
- GLSL语言基础
- 流式布局(一)
- 输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
- 两台Linux之间传输文件
- java设计模式之装饰者模式
- 安卓APP之加固技术