题解 | 多项式输出

多项式输出

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

大史

import sys
import math
import queue
import itertools
import heapq
from collections import deque
from array import array
from bisect import bisect_right


def read():
    line = sys.stdin.readline()
    if not line:
        return []
    return list(map(int, line.split()))


def readf():
    line = sys.stdin.readline()
    if not line:
        return []
    return list(map(float, line.split()))


mod = 10**9 + 7
inf = 10**18


def solve():
    n = read()[0]
    a = read()
    now = n + 1
    for x in a:
        now -= 1
        if now == 0:
            if x > 0:
                print("+", x, sep='')
            elif x < 0:
                print(x)
            continue
        if now == 1:
            if x==0:
                continue
            if x == 1:
                print("+x", end='')
            elif x == -1:
                print("-x", end='')
            elif x > 0:
                print("+{}x".format(x), end='')
            else:
                print("{}x".format(x), end='')
            continue

        if x != 0:
            if now == n:
                if x == 1:
                    print("x^{}".format(now), end='')
                elif x == -1:
                    print("-x^{}".format(now), end='')
                else:
                    print("{}x^{}".format(x, now), end='')
            else:
                if x == 1:
                    print("+x^{}".format(now), end='')
                elif x == -1:
                    print("-x^{}".format(now), end='')
                elif x > 0:
                    print("+{}x^{}".format(x, now), end='')
                else:
                    print("{}x^{}".format(x, now), end='')


def main():
    t = 1
    # t = int(sys.stdin.readline())
    for _ in range(t):
        solve()


if __name__ == "__main__":
    main()

全部评论

相关推荐

头像
10-15 15:44
已编辑
湖南大学 安卓
肖先生~:刚还和我说自己写好了但是忘记保存了,明天再给我,真服了,每次都是一样的说辞
你见过哪些工贼行为
点赞 评论 收藏
分享
11-04 19:05
已编辑
东莞城市学院 单片机
不知道怎么取名字_:你这个要实习两年?哪有这么久的,感觉就是即使你毕业了,但还按实习的话,是不是不用给你缴社保公积金啥的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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