洛谷P1067 [NOIP2009 普及组] 多项式输出

一开始的50分代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
using namespace std;

template <typename T>
inline T read()
{
    T x = 0;
    T f = 1; char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
       x = x * 10 + ch - '0';
       ch = getchar();
    }
    return x * f;
}

int x, cnt;

int main()
{
    cin >> cnt;
    for(int i = cnt; i >= 0; i--)
    {
        cin >> x;
        if(x == 0)
            continue;
        int t = abs(x);
        if(x > 0 && i < cnt)
            cout << "+";
        else if(x < 0)
            cout << "-";
        if(t > 1 || i == 0)
            cout << t;
        if(i > 0)
            printf("x^%d", i);
    }
    return 0;
}

 错误之处在于:

在x为一次方的时候,会输出x^1;修改后的正确代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#define For(i, j, n) for(int i = j ; i <= n ; ++i)
using namespace std;

template <typename T>
inline T read()
{
    T x = 0;
    T f = 1; char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
       x = x * 10 + ch - '0';
       ch = getchar();
    }
    return x * f;
}

int x, cnt;

int main()
{
    cin >> cnt;
    for(int i = cnt; i >= 0; i--)
    {
        cin >> x;
        if(x == 0)
            continue;
        int t = abs(x);
        if(x > 0 && i < cnt)
            cout << "+";
        else if(x < 0)
            cout << "-";
        if(t > 1 || i == 0)
            cout << t;
        if(i > 0)
        {
            cout << "x";
            if(i > 1)
                cout << "^" << i;
        }
    }
    return 0;
}

 

posted @ 2024-07-26 03:36  Gold_stein  阅读(7)  评论(0编辑  收藏  举报