UVA392 模擬(多項式輸出),水

1 題意,輸入一個多項式,輸出它。

2 分析。

注意模擬題思路和代碼都要按部就班、步驟條理清晰,一個姿勢過不了,如果覺得有更清晰的思路,就不要糾結過不了代碼,而是換個姿勢重新來一發。多想想題目邊界、自己代碼分類的邊界、以及0和1等特殊情況。

3

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

const int maxn=10;
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int f[maxn];
    memset(f,0,sizeof(f));
    while(~scanf("%d",&f[8])){
        ///ini
        for(int i=7;i>=0;i--){
            scanf("%d",&f[i]);
        }

        ///print
        int st=0;
        for(int j=8;j>=0;j--){
            if(f[j]!=0){
                if(st==0){
                    st=1;
                    if(j!=0){
                        ///.base_number:(底數)
                        if(f[j]==1)     cout<<"";
                        else if(f[j]==-1) cout<<"-";
                        else  cout<<f[j];

                        ///.index:(指數)
                        if(j==1)    cout<<"x";
                        else    cout<<"x^"<<j;
                    }
                    else{
                        cout<<f[j];
                    }
                }
                else if(st==1){
                    if(j!=0){
                        ///.base_number:
                        if(f[j]==1)     cout<<" + ";
                        else if(f[j]==-1)   cout<<" - ";
                        else if(f[j]>1)     cout<<" + "<<f[j];
                        else if(f[j]<-1)    cout<<" - "<<-1*f[j];

                        ///.index:
                        if(j==1)    cout<<"x";
                        else    cout<<"x^"<<j;
                    }
                    else{
                        if(f[j]>0)  cout<<" + "<<f[j];
                        else if(f[j]<0) cout<<" - "<<-1*f[j];
                    }
                }
            }
        }
        //cout<<endl; presentation error

        if(st==0)   cout<<"0"<<endl;
        else    cout<<endl;

        memset(f,0,sizeof(f));
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章