3028. 构造多项式

Li Dao

我竭尽全力能写出来的最简洁的方法

include

using namespace std;
int T;
int a[9];
void print(int ci,int ff)
{
if((!ff && a[ci]>0) || a[ci]<0) cout<<(a[ci]>0?’+’:’-‘);
if(!(ci>=1 && abs(a[ci])==1)) cout<<abs(a[ci]);
if(ci==1) cout<<’x’;
else if(ci>=2) printf(“x^%d”,ci);
return;
}
void solve()
{
int totzero=1;
for(int i=8;i>=0;i–) {cin>>a[i];if(a[i]!=0) totzero=0;}
if(totzero) cout<<0<=0;i–)
if(a[i]!=0)
{
print(i,ff);
if(ff) ff=0;
}
}
cout<<endl;
}
int main()
{
scanf(“%d”,&T);
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

Master X

BW题,注意各种特判就行……
mmp忘了把结尾换行符放出去……
写了个巨丑的代码……

include

using namespace std;
int main()
{
int T;
cin>>T;
for(int i=0;i<T;i++)
{
int a[9];
for(int j=0;j<9;j++) cin>>a[j];
int k=0;
printf(“case #%d:\n”,i);
for(int j=0;j<9;j++){
if(a[j]==0&&j!=8) continue;

        if(a[j]<0)
             {cout<<"-";a[j]=-1*a[j];}
        else if(a[j]>0&&k==1) cout<<"+";
        if(j!=8)
        {if(a[j]==1) {cout<<"x";if(8-j!=1) cout<<"^"<<8-j;}
        else {cout<<a[j]<<"x";if(8-j!=1) cout<<"^"<<8-j;}}
        else if(k==0||a[j]!=0) cout<<a[j];
        if(a[j]!=0) k=1;

    }
    cout<<endl;
}

}

LzQuarter
#include<iostream>
#include<queue>
using namespace std;
typedef struct _{
    int val;
    int ci;
}item;
int printed = 0;
void print(item t){
    if(printed && t.val > 0){
        cout << "+";
    }
    printed++;
    if(t.val < 0){
        t.val = 0 - t.val;
        cout << "-";
    }
    if(t.val != 1){
        cout << t.val;
    }

    if(t.ci == 0){
        if(t.val == 1){
            cout << t.val;
        }
    }
    else if(t.ci == 1){
        cout << "x";
    }
    else{
        cout << "x^" << t.ci;
    }
    return;
}
int main(){
    int T;
    cin >> T;
    for(int t = 0; t < T; t++){
        cout << "case #" << t << ":" << endl;
        printed = 0;
        queue<item> iq;
        for(int i = 8; i >= 0; i--){
            item temp;
            cin >> temp.val;
            temp.ci = i;
            if(temp.val != 0)
                iq.push(temp);
        }
        if(iq.empty()){
            cout << "0";
        }
        else{
            while(!iq.empty()){
                print(iq.front());
                iq.pop();
            }
        }
        cout << endl;
    }
    return 0;
}
HongRed

因为输入固定, 还是蛮简单的, 只要关注一下系数的0, 1, -1, 次数的0, 1和开头判断就可以了.

#include<cstdio>
using namespace std;
int main()
{
#ifdef debug
    freopen("testdata.in", "r", stdin);
    freopen("testdata.out", "w", stdout);
#endif
    unsigned T;
    scanf("%u", &T);
    for(unsigned e = 0; e < T; e++)
    {
        printf("case #%u:\n", e);
        bool output = false;
        int co;
        for(unsigned i = 8; i >= 0; i--)
        {
            scanf("%d", &co);
            if(co == -1) printf("-");
            else if(co == 1)
            {
                if(output) printf("+");
            }
            else if(co)
            {
                if(output) printf("%+d", co);
                else printf("%+d", co);
            }
            if(co)
            {
                printf("x");
                if(i != 1) printf("^%d", i);
            }
            output = output || co;
        }
        scanf("%d", &co);
        if(co)
        {
            if(output)printf("%+d", co);
            else printf("%d", co);
        }
        output = output || co;
        if(!output) printf("0");
    }
    return 0;
}
你当前正在回复 博客/题目
存在问题!