zoukankan      html  css  js  c++  java
  • P1067多项式输出

    这道题是2009普及组的题,仍然是一个字符串+模拟。(蒻到先不刷算法)

    这道题的题干给了很多的提示,也很全面,但是当我把种种情况都考虑到了后,在写代码的过程中仍然出现了很多的错误,wa了三四次。其实导致这些错误的缘由仍然是思路不够清晰,没有考虑清楚,以及代码能力差。多亏了测试数据,帮助我改错,,,

    1.模拟题,将情况准确分类判断

    2.考虑各个部分的共性,减去不必要的判断

    3.自己造特殊数据去验证,举一反三

    代码

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<cstring>
    #define MAXN 100005
    using namespace std;
    int n;
    string s;
    int a[MAXN];
    bool judge1(int x){//次数||系数是否为0 
        if(x==0) return true;
        else return false;
    }
    bool judge2(int x){//是否为负 
        if(x<0) return true;
        else return false; 
    }
    bool judge3(int x){//次数系数是否为一 
        if(x==1||x==-1) return true;
        else return false;
    } 
    int main(){
        cin>>n;
        for(int i=1;i<=n+1;i++){
            cin>>a[i];
        }
        int k=n;
        while(k>=0){
            if(judge1(a[n-k+1])==false){//系数不为0 
                if(k==n){//第一个特判 
                    if(judge2(a[n-k+1])){//负的 
                        if(judge3(a[n-k+1])==true){//系数为1 
                            cout<<"-"<<"x"<<"^"<<k;
                        }
                        else{
                            cout<<"-"<<abs(a[n-k+1])<<"x"<<"^"<<k;
                        }    
                    } 
                    else {//正的 
                        if(judge3(a[n-k+1])){
                            cout<<"x"<<"^"<<k;
                        }
                        else{
                            cout<<a[n-k+1]<<"x"<<"^"<<k;
                        }                            
                    }
                }
                else if(k==0){//为最后一个数
                    if(judge2(a[n-k+1])) cout<<"-"<<abs(a[n-k+1]);
                    else cout<<"+"<<a[n-k+1];                 
                    
                }
                else if(k==1){//次数为1时 
                    if(judge2(a[n-k+1])){//负的 
                        if(judge3(a[n-k+1])){
                            cout<<"-"<<"x";
                        }
                        else{
                            cout<<"-"<<abs(a[1+n-k])<<"x";
                        }
                    } 
                    else{//正的 
                        if(judge3(a[n-k+1])){
                            cout<<"+"<<"x";
                        }
                        else{
                            cout<<"+"<<a[n-k+1]<<"x"; 
                        }
                    }                                     
                } 
                else{//为后面的数 
                    if(judge2(a[n-k+1])){//负的 
                        if(judge3(a[n-k+1])){
                            cout<<"-"<<"x"<<"^"<<k;
                        }
                        else{
                            cout<<"-"<<abs(a[1+n-k])<<"x"<<"^"<<k;
                        }
                    } 
                    else{//正的 
                        if(judge3(a[n-k+1])){
                            cout<<"+"<<"x"<<"^"<<k;
                        }
                        else{
                            cout<<"+"<<a[n-k+1]<<"x"<<"^"<<k; 
                        }
                    }                     
                }        
            }
            k--;
        }    
        return 0;
        
    }
    待到oi十一月,我花开后百花杀。
  • 相关阅读:
    jython resources
    Installing a Library of Jython ScriptsPart of the WebSphere Application Server v7.x Administration Series Series
    jython好资料
    ulipad install on 64bit win7 has issue
    an oracle article in high level to descibe how to archtichre operator JAVA relevet project
    table的宽度,单元格内换行问题
    Linux常用命令大全
    dedecms系统后台登陆提示用户名密码不存在
    登录织梦后台提示用户名不存在的解决方法介绍
    Shell常用命令整理
  • 原文地址:https://www.cnblogs.com/china-mjr/p/11219364.html
Copyright © 2011-2022 走看看