多项式输出

来源:互联网 发布:联想优化os初始值 编辑:程序博客网 时间:2024/05/22 09:40

前言
欢迎来到嘟嘟老师的搞事情派对。又给我的博访问量+1。记得在下面给嘎嘎老师点赞哦!今天,嘟嘟老师给大家讲一道略稍复杂的题——多项式输出。可能四升五和五升六的小盆友们都不懂此题,因为有些是初中和六年级的专用名词。如果你是个大喷友,还没读懂题的话,那就再读几遍。
题目描述
一元 n 次多项式可用如下的表达式表示:
这里写图片描述
这里写图片描述
1. 多项式中自变量为x,从左到右按照次数递减顺序给出多项式。
2. 多项式中只包含系数不为0 的项。
3. 如果多项式n 次项系数为正,则多项式开头不出现“+”号,如果多项式n 次项系 数为负,则多项式以“-”号开头。
4. 对于不是最高次的项,以“+”号或者“-”号连接此项与前一项,分别表示此项 系数为正或者系数为负。紧跟一个正整数,表示此项系数的绝对值(如果一个高于0 次的项, 其系数的绝对值为1,则无需输出1)。如果x 的指数大于1,则接下来紧跟的指数部分的形 式为“x^b”,其中b 为x 的指数;如果x 的指数为1,则接下来紧跟的指数部分形式为“x”; 如果x 的指数为0,则仅需输出系数即可。
5. 多项式中,多项式的开头、结尾不含多余的空格。
输入
输入文件名为 poly.in,共有2 行
第一行 1 个整数,n,表示一元多项式的次数。
第二行有 n+1 个整数,其中第i 个整数表示第n-i+1 次项的系数,每两个整数之间用空 格隔开。
输出
输出文件 poly.out 共1 行,按题目所述格式输出多项式。
数据范围限制
1 ≤ n ≤ 100,多项式各次项系数的绝对值均不超过100。
样例输入1
5
100 -1 1 -3 0 10
样例输出1
100x^5-x^4+x^3-3x^2+10
样例输入2
3
-50 0 0 1
样例输出2
-50x^3+1
思路
(嘟嘟课堂开讲了)
读懂题的孩纸们一定知道嘟嘟老师为什么说此题略稍复杂,这里我就不多说了。这题是很搞事情的一道题。虽说复杂,但其实就在考你的循环,条件分支——if和你做题的耐心。要打很多if,一个循环直接搞定。很暴力很烦的一道题。详细思路请看代码,相信童鞋们都能看得懂,因为这题并不高深,只是很搞事情而已。
代码

var        a:array[0..10000] of longint;        i,n:longint;begin        assign(input,'poly.in');reset(input);        assign(output,'poly.out');rewrite(output);        read(n);        for i:=n downto 0 do        begin                read(a[i]);                if (i=n) then                begin                        if (a[i]>0) then                        begin                                if (a[i]=1) then write('x^',i) else write(a[i],'x^',i);                        end;                        if (a[i]<0) then                        begin                                if (a[i]=-1) then write('-x^',i) else write('-',abs(a[i]),'x^',i);                        end;                end;                if (i<>0) and (i<>n) and (i<>1) then                begin                        if (a[i]>0) then                        begin                                if (a[i]=1) then write('+x^',i) else write('+',a[i],'x^',i);                        end;                        if (a[i]<0) then                        begin                                if (a[i]=-1) then write('-x^',i) else write('-',abs(a[i]),'x^',i);                        end;                end;                if (i=1) then                begin                        if (a[i]>0) then                        begin                                if (a[i]<>1) then write('+',a[i],'x') else write('+x');                        end;                        if (a[i]<0) then                        begin                                if (a[i]<>-1) then write('-',abs(a[i]),'x') else write('-x');                        end;                end;                if (i=0) then                begin                        if (a[i]>0) then write('+',a[i]);                        if (a[i]<0) then write('-',abs(a[i]));                end;        end;        close(input);        close(output);end.
原创粉丝点击