首页 > 试题广场 >

多项式输出

[编程题]多项式输出
  • 热度指数:4192 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}给定一元 n 次多项式

f(x)=a_nx^n+a_{n-1}x^{n-1}+\dots+a_1x+a_0

\hspace{15pt}其中 a_n\neq0,系数 a_i\ (0\leqq i\leqq n) 满足 -100\leqq a_i\leqq100

\hspace{15pt}请按如下规则将多项式输出为字符串:
\hspace{23pt}\bullet\, 从高次到低次依次输出;
\hspace{23pt}\bullet\, 系数为 0 的项完全省略
\hspace{23pt}\bullet\, 对于次数大于等于 1 的项,若其系数为 1-1,则省略系数的绝对值 1(常数项即使为 1-1 也应完整输出);
\hspace{23pt}\bullet\, 次数为 0 仅输出常数;次数为 1 输出 x;次数 \geqq2 输出 \texttt{x^} k
\hspace{23pt}\bullet\, 输出的第一个非零项(即最高次项)若系数为正,不输出前导加号;后续正系数项前需加 \texttt{+},负系数项加 \texttt{-}

输入描述:
\hspace{15pt}第一行输入整数 n\left(1\leqq n\leqq100\right),表示多项式次数。
\hspace{15pt}第二行输入 n+1 个整数 a_n,a_{n-1},\dots,a_0,依次为 n 次项到 0 次项(常数项)的系数。


输出描述:
\hspace{15pt}在一行输出格式化后的多项式字符串。
示例1

输入

5
100 -1 1 -3 0 10

输出

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

输入

3
-50 0 0 1

输出

-50x^3+1
头像 BraveCoder
发表于 2025-08-29 08:34:55
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); 展开全文
头像 手有余湘
发表于 2025-07-23 17:06:14
import sys time=int(input()) array=list(map(int,input().split())) result=[] for i in range(time+1): middle=[] #系数部分 if array[i]==0: 展开全文
头像 Z_L_G
发表于 2025-02-24 23:34:20
题意 给定多项式系数,输出多项式 思路 简单的模拟即可,注意如何划分模拟过程,先输出符号,再输出常数,最后输出指数。 不要贪心,不要想着一边读入一边输出,或者一次性同时处理符号,常数和指数 AC代码 #include<bits/stdc++.h> using namespace 展开全文
头像
发表于 2025-11-16 16:37:04
屎山代码 #include <iostream> using namespace std; int main() { int n; cin >> n; int a[n+2]; for (int i = 1; i <= n+1; i ++ 展开全文
头像 星璇Xx
发表于 2025-11-18 16:56:27
#include<bits/stdc++.h> using namespace std; int a[110]; int main(){ int n; cin >> n; for(int i=0 ; i <= n ; i++){ cin >> a[ 展开全文
头像 周康禧
发表于 2025-12-03 21:48:35
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; using PII=pair<ll,ll>; using PIII=pair< 展开全文
头像 彩虹味的Iris
发表于 2025-12-05 13:29:39
#include <bits/stdc++.h> using namespace std; //直接模拟所有情况 int main() { int n; cin >> n; int a[105]; string s; for (int 展开全文
头像 牛客43249513号
发表于 2025-11-29 09:15:42
#include <bits/stdc++.h> using namespace std; int n,a[105]; int main(){ cin>>n; for(int i=n;i>=0;i--){ int a; c 展开全文
头像 niuke9999
发表于 2025-11-16 10:46:01
#include <stdio.h> int main(void) { int n; scanf("%d", &n); int a[n + 1]; for (int i = n; i >= 0; i--) 展开全文
头像 Zhitong
发表于 2025-07-30 15:50:26
n = int(input()) a = list(map(int, input().split())) def get_poly(i): # 处理系数部分 coeff = a[len(a)-1-i] if coeff == 0: return None 展开全文