首页 > 试题广场 >

多项式输出

[编程题]多项式输出
  • 热度指数:4766 时间限制: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
tactic:分类讨论,注意细节
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    int t = n + 1;
    vector<int>nums(t);
    for (int i = 0; i < t; i++) {
        cin >> nums[i];
    }
    reverse(nums.begin(), nums.end());
    for (int i = n; i >= 0; i--) {
        if (i == n) {
            if (nums[i] != 0) {
                if(nums[i] == 1){
                    cout<<"x^"<<i;
                }
                else if(nums[i] == -1){
                    cout<<'-'<<"x^"<<i;
                }
                else cout << nums[i] << "x^" << i;
            }
        }
        else if (i == 0) {
            if (nums[i] > 0) {
                cout << '+' << nums[i] << '\n';
            }
            if (nums[i] < 0) {
                cout << nums[i] << '\n';
            }
        }
        else if(i ==1){
            if (nums[i] > 0) {
                if(nums[i] == 1){
                    cout<<'+'<<'x';
                }
                else cout << '+' << nums[i] << 'x';
            }
            if (nums[i] < 0) {
                if(nums[i] == -1){
                    cout<<'-'<<'x';
                }
                else cout << nums[i] << 'x';
            }
        }
        else {
            if (nums[i] > 0) {
                if(nums[i] == 1){
                    cout<<'+'<<"x^"<<i;
                }
                else cout << '+' << nums[i] << "x^" << i;
            }
            if (nums[i] < 0) {
                if(nums[i] == -1){
                    cout<<'-'<<"x^"<<i;
                }
                else cout << nums[i] << "x^" << i;
            }
        }
        
    }

}
// 64 位输出请用 printf("%lld")
发表于 2026-01-09 10:39:58 回复(0)
from re import split
import sys

n = int(input())
num_list = list(map(int, input().split())) #整数列表
a = []
for i in range(n, 1,-1):
    a.append(f"x^{i}")
a.append('x')

b = []
for i in range(n): #常数项先忽略
    if num_list[i] == 0:
        continue
    elif num_list[i] == 1:
        b.append('+' + a[i])
    elif num_list[i] == -1:
        b.append('-' + a[i])
    else:
        if num_list[i] > 0:
            b.append('+'+ str(num_list[i]) + a[i])
        else:
            b.append(str(num_list[i]) + a[i])
# 考虑常数项
if num_list[n] == 0:
    pass
elif num_list[n] > 0:
    b.append('+' + str(num_list[n]))
else:
    b.append(str(num_list[n]))

c = ''.join(b)
if c[0] =='+':
    c = c[1:]

print(c)


发表于 2025-09-19 23:30:42 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取多项式次数
        int n = scanner.nextInt();

        // 读取系数,顺序是a_n, a_{n-1}, ..., a_0
        int[] coefficients = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            coefficients[i] = scanner.nextInt();
        }

        scanner.close();

        // 构建多项式字符串
        StringBuilder result = new StringBuilder();
        boolean isFirstTerm = true;

        // 从高次到低次处理每一项
        for (int i = 0; i <= n; i++) {
            int coefficient = coefficients[i];
            int degree = n - i; // 当前项的次数

            // 系数为0的项完全省略
            if (coefficient == 0) {
                continue;
            }

            // 处理符号
            if (isFirstTerm) {
                // 首项若系数为正,不输出前导"+"
                if (coefficient < 0) {
                    result.append("-");
                }
                isFirstTerm = false;
            } else {
                // 后续正系数项前需加"+",负系数项加"-"
                if (coefficient > 0) {
                    result.append("+");
                } else {
                    result.append("-");
                }
            }

            // 处理系数的绝对值
            int absCoeff = Math.abs(coefficient);
            // 当系数为1或-1且次数≥1时,省略系数的绝对值1
            if (!(absCoeff == 1 && degree >= 1)) {
                result.append(absCoeff);
            }

            // 处理变量部分
            if (degree > 0) {
                result.append("x");
                // 次数为1输出"x";次数≥2输出"x^k"
                if (degree > 1) {
                    result.append("^").append(degree);
                }
            }
            // 次数为0仅输出常数,这里不需要额外处理
        }

        System.out.println(result.toString());
    }
}

发表于 2025-08-29 08:34:39 回复(0)
n = int(input())
a = list(input().split())

for i in range(n):
    flag = '^'+str(n-i)
    if i==n-1:
        flag = ''
    if a[i]=='-1':
        a[i] = '-'+'x'+flag
    elif a[i]=='1' and i!=0:
        a[i] = '+'+'x'+flag
    elif a[i]=='0':
        a[i] =''
    elif int(a[i])<0:
        a[i] += 'x'+flag
    else:
        if i==0:
            if a[i]=='1':
                a[i] = 'x'+flag
            else:
                a[i] += 'x'+flag
        else:
            a[i] = '+'+a[i]+'x'+flag

if int(a[-1])>0:
    a[-1] = '+'+a[-1]
elif int(a[-1])==0:
    a[-1] = ''

print(''.join(a))
发表于 2025-08-25 12:03:54 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n; cin >> n;
    vector<int> vi(n+1);
    vector<bool> vb(n+1, false);
    string ans;

    for(int i = n; i >= 0; --i){
        cin >> vi[i];
        if(vi[i] < 0) {vb[i] = true; vi[i] *= -1;}
    }

    for(int i = n; i >= 0; --i){
        if(vi[i] == 0) {continue;}

        if(vb[i]) {cout << "-";}
        else if(i != n){cout << "+";}

        if(vi[i] != 1) {cout << vi[i];}
        if(i > 1) {cout << "x^" << i;}
        else if(i == 1) {cout << "x";}
    }

    if(vi[0] == 1) {cout << 1;}
   
    return 0;
}
发表于 2025-12-24 16:09:42 回复(0)
//vs可过
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> ary;
    int N = n + 1;
    while (N--) {
        int x;
        cin >> x;
        ary.push_back(x);
    }
    string s = "\n";
    if (ary[0] == -1) {
        s = "-x^" + to_string(n);
    }
    else if (ary[0] == 1) {
        s = "x^" + to_string(n);
    }
    else if (ary[0] == 0) {
        s = "\0";
    }
    else {
        if (ary[0] > 1) {
            s = to_string(ary[0]) + "x^" + to_string(n);
        }
        else if (ary[0] < -1) {
            s = to_string(ary[0]) + "x^" + to_string(n);
        }
    }

    for (int x = 1, y = n - 1; x < n - 1; x++, y--) {
        if (ary[x] == -1) {
            s += "-x^" + to_string(y);
        }
        else if (ary[x] == 1) {
            s += "+x^" + to_string(y);
        }
        else if (ary[x] == 0) {
            continue;
        }
        else {
            if (ary[x] > 1) {
                s += "+" + to_string(ary[x]) + "x^" + to_string(y);
            }
            else if (ary[x] < -1) {
                s += to_string(ary[x]) + "x^" + to_string(y);
            }
        }
    }

    if (ary[n - 1] == -1) {
        s += "-x";
    }
    else if (ary[n - 1] == 1) {
        s += "+x";
    }
    else if (ary[n - 1] == 0) {
        s += '\0';
    }
    else {
        if (ary[n - 1] > 1) {
            s += "+" + to_string(ary[n - 1]) + "x";
        }
        else if (ary[n - 1] < -1) {
            s += to_string(ary[n - 1]) + "x";
        }
    }

    if (ary[n] == -1) {
        s += "-1";
    }
    else if (ary[n] == 1) {
        s += "+1";
    }
    else if (ary[n] == 0) {
        s += '\0';
    }
    else {
        if (ary[n] > 1) {
            s += "+" + to_string(ary[n]);
        }
        else if (ary[n] < -1) {
            s += to_string(ary[n]);
        }
    }


    cout << s << endl;
}
发表于 2025-08-08 18:37:49 回复(0)
#include <climits>
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;

    for (int i = n; i >= 0; i--) {
        int a;
        char k;
        cin >> a;

        if(a==0) continue;

        // 1.加号问题
        if(i!=n&&a>0){
            cout<<'+';
        }

        // 2.系数问题
        if(i!=0 && (a==-1 || a==1)){
            if(a==-1) cout<<'-';
        }else{
            cout<<a;
        }

        // 3. 指数问题
        if(i==1){
            cout<<'x';
        }else if(i==0){

        }else{
            cout<<'x'<<'^'<<i;
        }




    }
}
// 64 位输出请用 printf("%lld")
发表于 2025-08-05 14:07:54 回复(0)