P1067 [NOIP2009 普及组] 多项式输出(模拟输出细节)

Description of the topic

A dollar nn polyn can be represented by an expression such as:

f (x)\a_nx=a_ n-1]x=n-1]]cdos=a_1x=a_0,a_n=n=anxn=an=1xn=1=a=a=a0,=0]

Among them, the a_ix is called the secondary term, a_iai is called the coefficient of the secondary. Given the number and coefficient of a unitary polynomial item, output the polynomial as specified in the following format:

  1. The argument in the polynomial is xx, and the polynomial is given from left to right in decreasing order of number of times.

  2. The polynomial contains only items with a factor of not 00.

  3. If the polyn sub-factor is positive, the polynomial does not begin with a "plus" sign, and if the polyn nn sub-factor is negative, the polynomial begins with a "-".

For items that are not the highest, connect the item with the previous item with a "plus" sign or a "-" sign, indicating that the coefficient is positive or negative, respectively. Follow a positive integer that represents the absolute value of the coefficient (if an item is more than 00 times, the absolute value of the coefficient is 11, no output 11 is required). If the index of xx is greater than 11, the index portion that follows is "x-bxb", where bb is the index of xx, if the index of xx is 11, the index part of the index that follows is "xx", and if the index of xx is 00, only the output coefficient is required.

In a polynomial, there are no extra spaces at the beginning or end of the polynomial.

Enter the format

Input common 2 lines

Line 1 integers,n, representing the number of times a single polynomial.

The second line has an integer of n-plus, where the second integer represents the coefficient of the n-i-1 term, separated by a space between each of the two integers.

The output format

The output totals 1 line, output polynomials in the format described in the title.

A sample of the input and output

Enter #1 copy

5 
100 -1 1 -3 0 10

Output #1 copy

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

Enter #2 copy

3 
-50 0 0 1 

Output #2 copy

-50x^3+1 

Description/Tips

NOIP 2009 Popularization Group First Question

For 100% of the data, 0 sle n sle 1000 ≤n≤100, -100 sle s100 ≤ coefficient sle 100≤100

	#include<bits/stdc++.h>
	using namespace std;
	int main()
	{
		int n,m;
		cin>>n;
		for(int i=n;i>=0;i--)
		{
			cin>>m;
			if(m)//把这个为0的情况直接不输出 
			{if(i!=n&&m>0)cout<<"+";//第一位不能有加号 
			if(abs(m)>1||i==0)cout<<m;//m的绝对值不为1和0; 直接输出m 
			if(m==-1&&i)cout<<"-";// 不是最后 一位,把1删掉 
			if(i>1)cout<<"x^"<<i;
			if(i==1)cout<<"x";//特殊化 
			}
		}
		
	 } 

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 题目描述 给定一个多项式 $P(x)$,请你输出它的形式。 输入格式 第一行包含一个整数 $n$,表示多项式的项数。 接下来 $n$ 行,每行包含两个整数 $a_i$ 和 $b_i$,表示多项式中某一项的系数和指数。 输出格式 共一行,按照指数从高到低的顺序输出多项式。 每一项格式为 $a\times x^b$,其中 $a$ 是系数,$b$ 是指数。 如果某一项系数为 $0$,则不输出该项。 如果所有项系数均为 $0$,则输出 $0$。 数据范围 $0≤n≤100$, $0≤a_i≤100$, $0≤b_i≤1000$ 输入样例1: 3 -1 2 1 1 -1 0 输出样例1: -1*x^2+1*x^1-1 输入样例2: 1 0 0 输出样例2: 0 算法1 (模拟) $O(n)$ 时间复杂度 参考文献 python3 代码 C++ 代码 算法2 (暴力枚举) $O(n^2)$ blablabla 时间复杂度 参考文献 C++ 代码 ### 回答2: 题目描述: 给定一个多项式 f(x),其中系数可以为负数,且已知 f(x) 的次数和所有系数的值,要求将 f(x) 展开后的式子输出。 输入格式: 第一行包含整数 n,表示多项式的次数。 第二行包含 n+1 个整数,表示多项式的系数,其中第 i 个数表示 ai−1(i=1,2,…,n+1)。 输出格式: 输出一行,表示展开后的式子。 多项式展开有以下规则: 1. x 的k次方展开为 x^k 2. -1 的 k 次方展开为 (-1)^k 3. 定义 +和-运算时,需要分情况讨论,具体为: 情况1:加号(+),若系数为正,则直接输出加号+和系数。 情况2:加号(+),若系数为负,则直接输出系数(注意,此时不需要加+号)。 情况3:减号(-),若系数为正,则输出减号-和系数(注意,此时不需要加+号)。 情况4:减号(-),若系数为负,则输出减号-和系数的绝对值(注意,此时不需要加+号)。 4. x^k的系数为 1,如果k=0,则只输出1,如果k=1,则只输出x,否则输出x^k。 5. -1的k次方的系数也为1,如果k=0,则只输出1,如果k=1,则只输出-1,否则输出(-1)^k。 解题思路: 首先是要读入多项式系数数,通过 FOR 循环将其读入,存于数 A[] 中,同时计算并存储它的次数 n,如果某个系数是负数,也不要忘记把它的符号取出。 通过分情况讨论可以得知,加减强制使用 IF-ELSE 函数,同时通过输出中先前提取出的符号以及系数,进行处理。具体过程可以看代码。 最后还要特别注意的是:输出的第一项不可以有正号,全部列出来也不行!如果正好是第一项的话怎么办?我们运用了flag(标记),只有当不是最前面的一项才输出加号。 AC代码: ### 回答3: 题目简述 多项式是一个数学上常见的概念,指的是由多个单项式相加或相乘而成的表达式。多项式可以用一维数来表示,数的每个元素表示对应项的系数,下标表示对应项的次数。例如,多项式 $f(x)=2x^3-3x+1$ 可以表示为数 $a=[1,0,-3,2]$,其中 $a[0]$ 表示常数项系数,$a[1]$ 表示 $x$ 的系数,$a[2]$ 表示 $x^2$ 的系数,以此类推。本题要求将多项式转化为字符串输出,规定输出格式为“$ax^k$”,其中 $a$ 为系数(有可能为负数),若 $k>1$ 则字符串中还需包含 $k$ 的文字表示,当 $k=1$ 或 $a=0$ 时则只输出 $x$ 或 $a$ 的绝对值。 题目分析 读完题目后,我们可以发现此题非常简单明了,需要我们将多项式转化为字符串输出,只需要遍历多项式,判断系数是否为 0,若不为 0 则将系数转化成字符串并存入输出字符串中,然后再判断当前项是不是常数项、一次项或高次项,进行不同的输出即可。 但是需要注意一些细节问题。首先,对于负系数,我们需要在输出时加上负号“-”;其次,要求当系数为 1 时不输出系数,系数为 -1 时只输出负号“-”;还有,对于高次项,要输出 k 的文字表示,例如“x^3”,而且需要注意一次项和常数项的情况。最后,输出时还需要注意字符串格式的问题,要在每个“$ax^k$”后面加上“+”或“-”。综上所述,此题较为简单,主要就是需要考虑一些细节问题。 代码实现 下面是代码实现。为了方便输出,我们可以使用 C++ 中的 stringstream 类,将输出的内容逐步存入 stringstream 对象中,最后输出时再将其转化为字符串。另外,我们可以写一个辅助函数 print_xk,负责将给定的次数 k 输出成“x^k”的形式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法编程张老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值