题解 | #多项式输出#

多项式输出

https://ac.nowcoder.com/acm/problem/16622

分别对 n+1 项进行判断,需要考虑的情况较多,有:

  1. 系数为 0 时,不需要输出该项;
  2. 系数为正且该项不是第一个输出项时,需要输出 +
  3. 系数为负时,需要输出 -
  4. 系数绝对值为 1 且不为常数项时,不需要输出系数;
  5. 常数项不需要输出 x 和指数。
  6. 非常数项指数为 1 时,不需要输出指数。
#include <bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;
    int a[n + 1];
    for (int i = 0; i <= n; i++)
        cin >> a[i];
    bool flag = false;
    for (int i = 0; i <= n; i++)
    {
        if (a[i] == 0)
            continue;
        if (a[i] > 0 && flag)
            cout << "+";
        if (a[i] < 0)
            cout << "-";
        if (abs(a[i]) != 1 || i == n)
            cout << abs(a[i]);
        if (i < n - 1)
            cout << "x^" << n - i;
        if (i == n - 1)
            cout << "x";
        flag = true;
    }
    cout << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    int t;
    // cin >> t;
    t = 1;
    while (t--)
        solve();
    return 0;
}
全部评论

相关推荐

03-05 17:03
已编辑
浙江工商大学 C++
陈好好wy:整体看下来有点空空的感觉,可以把每一段项目经历都再完善一下,然后用小标题的形式写个两到三条,目前看有点太简单了,不太能看出具体在这个项目里做了什么工作。还是要尽量把自己做的工作以量化的形式体现在简历上呢。
双非本科求职如何逆袭
点赞 评论 收藏
分享
程序员小白条:现在这个简历很没竞争力,而且很多都不要28届的,基本就看运气了,如果没简历包装的话,就海投中小厂吧
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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