一元 n 次多项式可用如下的表达式表示:



其中,aixi 称为i次项,ai称为i次项的系数。给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式:

  1. 多项式中自变量为x,从左到右按照次数递减顺序给出多项式。
  2. 多项式中只包含系数不为0 的项。

  3. 如果多项式n 次项系数为正,则多项式开头不出现“+”号,如果多项式n 次项系数为负,则多项式以“-”号开头。

  4. 对于不是最高次的项,以“+”号或者“-”号连接此项与前一项,分别表示此项系数为正或者系数为负。紧跟一个正整数,表示此项系数的绝对值(如果一个高于0 次的项,其系数的绝对值为1,则无需输出1)。如果x 的指数大于1,则接下来紧跟的指数部分的形式为“x^b”,其中b 为x 的指数;如果x 的指数为1,则接下来紧跟的指数部分形式为“x”; 如果x 的指数为0,则仅需输出系数即可。

  5. 多项式中,多项式的开头、结尾不含多余的空格。

    格式

    输入格式

共有2 行。

第一行 1 个整数,n,表示一元多项式的次数(1 ≤ n ≤ 100)。

第二行有 n+1 个整数,其中第i 个整数表示第n-i+1 次项的系数,每两个整数之间用空格隔开。

多项式各次项系数的绝对值均不超过100。

输出格式

共1 行,按题目所述格式输出多项式。

样例1

样例输入1[复制]

5

100 -1 1 -3 0 10

样例输出1[复制]

100x^5-x^4+x^3-3x^2+10

限制

每个测试点1s。

【题目链接】:

【题解】



题中故意隐藏了x的指数为1的情况。这种情况直接输出x就好不要输出x^1,然后系数为1或负1都省略掉,系数为0就跳过不输出;

指数为n和指数为0的情况特判一下就好;



【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second typedef pair<int,int> pii;
typedef pair<LL,LL> pll; void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} const int MAXN = 2e2;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n;
int a[MAXN]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep2(i,n,0)
rei(a[i]);
if (a[n]<0)
putchar('-');
a[n] = abs(a[n]);
if (a[n]!=1)
printf("%d",a[n]);
putchar('x');
if (n!=1)
printf("^%d",n);
rep2(i,n-1,1)
{
if (a[i]<0)
putchar('-');
else
if (a[i]>0)
putchar('+');
else
continue;
a[i] = abs(a[i]);
if (a[i]!=1)
printf("%d",a[i]);
putchar('x');
if (i!=1)
printf("^%d",i);
}
if (a[0]<0)
putchar('-');
else
if (a[0]>0)
putchar('+');
else
return 0;
printf("%d",abs(a[0]));
return 0;
}

【p091】多项式输出的更多相关文章

  1. 洛谷——P1067 多项式输出

    P1067 多项式输出 题目描述 一元 n 次多项式可用如下的表达式表示: 其中,aixi称为 i 次项,ai 称为 i 次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该 ...

  2. 洛谷 P1067 多项式输出

    P1067 多项式输出 模拟,很坑的那种 var i,n:longint; a:array[1..105] of integer; begin readln(n); for i:=1 to n+1 d ...

  3. 【6.18校内test】T1多项式输出

    日常题前废话: 首先so amazing 的一件事,因为在洛谷上立下了的flag,然后这次考试前两道题都是刚刚做过不久的题emmm(相当于白送200吗qwq,但是这阻挡不了我第三题不会的脚步qwq) ...

  4. 洛谷P1067 多项式输出 NOIP 2009 普及组 第一题

    洛谷P1067 多项式输出 NOIP 2009 普及组 第一题 题目描述 一元n次多项式可用如下的表达式表示: 输入输出格式 输入格式 输入共有 2 行 第一行 1 个整数,n,表示一元多项式的次数. ...

  5. 多项式输出 (0)<P2009_1>

    多项式输出 (poly.pas/c/cpp) [问题描述] 一元n次多项式可用如下的表达式表示: 其中,称为i次项,ai称为i次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输 ...

  6. Java实现 蓝桥杯VIP 算法提高 多项式输出

    算法提高 多项式输出 时间限制:1.0s 内存限制:512.0MB 问题描述 一元n 次多项式可用如下的表达式表示: f(x)=a[n]xn+a[n-1]x(n-1)+-+a[1]x+a[0], a[ ...

  7. 【洛谷】P1067 多项式输出

    原题链接:P1067 多项式输出 题目分析:学长推荐的OJ网站 --洛谷,发现挺好用的还可以下载提交出错的数据. 废话就不多说了,这道题属于基础题.提交出错主要是因为一些小细节不到位,这里就不一一赘述 ...

  8. NOIP2009多项式输出(水)【A004】

    [A004]潜伏者[难度A]—————————————————————————————————————————————————————————————————————————— [题目要求] 一元 n ...

  9. TYVJ P1103 多项式输出 Label:模拟 有点儿坑

    描述 一元 n 次多项式可用如下的表达式表示:  f(x)=an*x^n+an-1*x^n-1+...+a1*x+a0,an<>0其中,ai*a^x 称为i次项,ai称为i次项的系数.给出 ...

随机推荐

  1. RecyclerView 展示多种类型Item数据

    一.多Item布局实现(MultipleItem) 如果之前你用过ListView实现过此功能,那么你一定对下面这两个方法并不陌生 @Override public int getItemViewTy ...

  2. 玲珑杯 Round 19 B Buildings (RMQ + 二分)

    DESCRIPTION There are nn buildings lined up, and the height of the ii-th house is hihi. An inteval [ ...

  3. Android 为什么要有handler机制?handler机制的原理

    为什么要有handler机制? 在Android的UI开发中,我们经常会使用Handler来控制主UI程序的界面变化.有关Handler的作用,我们总结为:与其他线程协同工作,接收其他线程的消息并通过 ...

  4. ORA-16009 remote archive log destination must be a STANDBY database

    ORA-16009错误处理 问题描述: 主备在做Switchover切换时,在切换后的备库报如下错误: Wed Jul 22 04:49:02 2015 Errors in file /u01/app ...

  5. javasciprt cookies 操作

    <script type="text/javascript"> function getCookie(c_name){ if (document.cookie.leng ...

  6. 51Nod——N1118 机器人走方格

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1118 基准时间限制:1 秒 空间限制:131072 KB 分值: 0  ...

  7. jquery验证篇

    jquery验证:验证不了(包括easy ui 插件引用的js) jquery easy ui 引用的js <span style="color:#000000;">& ...

  8. php字符串函数分类总结

    php字符串函数分类总结 一.总结 explode  str_split  str_word_count  strtolower 二.php字符串函数分类总结 php内置了98个字符串函数(除了基于正 ...

  9. GCJ 2009 Round 2 Problem A. Crazy Rows

    https://code.google.com/codejam/contest/204113/dashboard 题目大意: 给你一个矩阵,让你转化为下三角矩阵,每次只能交换相邻的行,求最小的交换次数 ...

  10. 优雅地使用Retrofit+RxJava(二)

    前言 在我上一篇讲Retrofit+RxJava在MVP模式中优雅地处理异常(一)中,发现非常多网友发邮箱给我表示期待我的下一篇文章,正好趁着清明假期.我就写写平时我在使用RxJava+Retrofi ...