【寒假任务】洛谷1067 多项式输出
2017-01-13 19:51
204 查看
问题描述
给出多项式的最高次项,再给出各次项的系数,要求将此多项式补全。
样例输入
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)。
![](https://img-blog.csdn.net/20170113195031414?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRmFsbGVuX0FuZ2VsMDAx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Pixiv ID:60209427
给出多项式的最高次项,再给出各次项的系数,要求将此多项式补全。
样例输入
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
相关文章推荐
- 洛谷 1067——多项式输出(简单的模拟)
- 洛谷1067 多项式输出 解题报告
- [洛谷1067]多项式输出
- 洛谷1067 多项式输出
- 【寒假任务】洛谷1125 笨小猴
- 洛谷——P1067 多项式输出
- 洛谷 P1067 多项式输出
- 【洛谷】P1067 多项式输出【模拟】
- 【寒假任务】 洛谷1177 快速排序
- 【寒假任务】 洛谷1059 明明的随机数
- |洛谷|NOIP2009|模拟|P1067 多项式输出
- 洛谷模拟多项式输出P1067
- 【寒假任务】 洛谷1781 宇宙总统
- 【寒假任务】 洛谷1051 谁拿了最多奖学金
- 【寒假任务】洛谷1003 铺地毯
- 洛谷 P1067 多项式输出
- 【寒假任务】 洛谷1223 排队接水
- 洛谷 P1067 多项式输出
- 【寒假任务】 洛谷1583 魔法照片
- 【寒假任务】 洛谷1068 分数线划定