题解 | 多项式输出

多项式输出

https://www.nowcoder.com/practice/142ee43d3e7345d385328faca9f636e5

#include <ios>
#include <iostream>
#include<vector>
#include<cmath>
using namespace std;

int main() {
    int n,sign=0;
    cin>>n;
    vector<int>arr;
    n+=1;
    while(n--){
        int temp;
        cin>>temp;
        if(temp<0)cout<<"-";
        else if(sign==1&&temp>0)cout<<"+";
        if(n>1){
            if(fabs(temp)>1)cout<<fabs(temp)<<"x^"<<n;
            else if(fabs(temp)==1)cout<<"x^"<<n;
        }
        else if(n==1){
            if(fabs(temp)>1)cout<<fabs(temp)<<"x";
            else if(fabs(temp)==1)cout<<"x";
        }
        else{
            if(temp!=0){
                cout<<fabs(temp);
            }
        }
        sign=1;
    }
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务