WA 了 Hack 数据求调

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

CB_X2_Jun @ 2024-08-06 19:49:26

#include <iostream>
using namespace std;
int n,s,x;
int main()
{
    cin >> n;
    s=n;
    for(int i=n; i>=1; i--)
    {
        cin >> x;
        if(i==s)
        {
            if(i==1)
            {
                if(x==0)continue;
                else if(x==1)cout << "x";
                else if(x==-1)cout << "-x";
                else cout << x << "x";
            }
            if(x==0)
            {
                s--;
                continue;
            }
            else if(x==1)cout << "x^" << i;
            else if(x==-1)cout << "-x^" << i;
            else
            {
                cout << x << "x^" << i;
            }
        }
        else
        {
            if(i==1)
            {
                if(x==0)continue;
                else if(x==1)cout << "+x";
                else if(x==-1)cout << "-x";
                else if(x<0)cout << x << "x";
                else cout << "+" << x << "x";
            }
            else if(x==0)
            {
                continue;
            }
            else if(x==1)cout << "+x^" << i;
            else if(x==-1)cout << "-x^" << i;
            else if(x<0)cout << x << "x^" << i;
            else cout << "+" << x << "x^" << i;
        }
    }
    cin >> x;
    if(n==0&&x==0)
    {
        cout << 0;
        return 0;
    }
    if(x>0)cout << "+" << x;
    else if(x<0) cout << x;
    return 0;
}

rt,\tt \color{82345F}Hack 之外的都能过。


by CB_X2_Jun @ 2024-08-06 19:51:55

Hack 是

0
1

我会输出

+1

????????


by CB_X2_Jun @ 2024-08-06 19:54:46

欸我好像懂了,我代码里

    cin >> x;
    if(n==0&&x==0)
    {
        cout << 0;
        return 0;
    }
    if(x>0)cout << "+" << x;
    else if(x<0) cout << x;

有问题吧???
我好像应该把 \tt \color{violet}if 改一下


by CB_X2_Jun @ 2024-08-06 19:59:51


by CB_X2_Jun @ 2024-08-06 20:00:32

事实证明根本就甭发讨论


by ouyangdou @ 2024-08-14 18:40:18

@CB_X2_Jun n=0时常数项为正数不用输出+,n=1时一次项系数为正数是不用输出+。


|