输入一个整数数组 ,打印出对应的多项式

比如输入[1,-1,3,5,0]

返回 5x^3+3x^2-x+1

即从第一个非零元素开始打印,如果系数为1则省略,如果指数为一,同样省略,如果系数为0,则该项跳过,如果指数为0,则指数部分省略,

第一版:

思路是分情况讨论,先用循环求出是否全零,同时求出第一个元素索引,在输出每一项时,将符号和后面的部分分开讨论,后面的系统取绝对值输出,其实一共就五种情况:

1指数为0,此时该项只有系数,已经包括系数为1的情况

2 指数为1同时系数为1,此时输出x

3 指数为1系数不为1,此时输出5x这样的

4 指数不为1,系数为1,此时输出x^2这样的

5 指数不为0,也不为1,系数不为1,此时输出5x^2这样

系数为0时直接跳过该项,第一版不足之处在于循环了两次

# your code goes here
def poly(coef):
    res = ""
    coef.reverse()
    n=len(coef)
    markzero=True    #定义是否全零,如有非零项,则为False
    markfir=0   #记录非零项元素索引,只有markzero为False时才有意义

    for i, d in enumerate(coef):   #循环获得第一个非零元素索引
        if d != 0:
            markzero = False
            markfir = i
            break
        else:
            continue
    if markzero == True:  #元素全为零则输出0
        res="0"
    else:
        for i,d in enumerate(coef):
            if d==0:    #系数为零跳过
                continue
            else:
                if d>0 and i!=markfir:   #系数非零且索引非第一个非零元素才输出前面的符号,因为可能前面几项都是零
                    res+="+"
                if d<0:
                    res += "-"
                if abs(d)==1:   #系数为1
                    if n-i-1==1:   #系数为1且指数为1
                        res += "x"
                    elif  n-i-1==0:  #系数为1且指数为0,其实包括在指数为0的情况里
                        res+="1"
                    else:
                        res+="x^%d" % (d)  #系数为1,指数不为0也不为1
                else:  #系数非1
                    if n-i-1==1:  # 指数为1
                        res += "%dx" % (abs(d))
                    elif  n-i-1==0:  #指数为0
                        res+="%d" % (abs(d))
                    else:    #一般情况
                        res+="%dx^%d" % (abs(d),n-i-1)
    print res
coef=[0,-1,0,0]

poly(coef)

第二版:

用一个循环实现功能,将对于数组是否全零的判断加入循环中,如果数组全零,则循环一直continue,此时markzero=True,只要有一个元素非零,则markzero=False,在记录第一个非零系数的索引时,先判断markzero,只有当第一次碰到非零元素时才改变markfir的值,这样就保存下来了第一个非零元素的索引,

记录第一个非零元素的索引,作用是第一个非零正系数数前面是不用加+号的。

# your code goes here
def poly(coef):
    res = ""
    coef.reverse()
    n=len(coef)
    markzero=True
    markfir=0

    for i,d in enumerate(coef):
        if d==0:
            continue
        if markzero:
            markfir=i
        markzero=False
        if d>0 and i!=markfir:
            res+="+"
        if d<0:
            res += "-"
        if abs(d)==1:
            if n-i-1==1:
                res += "x"
            elif  n-i-1==0:
                res+="1"
            else:
                res+="x^%d" % (d)
        else:
            if n-i-1==1:
                res += "%dx" % (abs(d))
            elif  n-i-1==0:
                res+="%d" % (abs(d))
            else:
                res+="%dx^%d" % (abs(d),n-i-1)
    if markzero:
        print "0"
    else:
        print res
coef=[1,-1,3,5,0]
poly(coef)

第三版,将每一项拆分为符号,系数和指数,在第四版中将后两项合并为指数项,这样的作用是思路更清晰

# your code goes here
def poly(coef):
    res = ""
    coef.reverse()
    n=len(coef)
    markzero=True
    markfir=0

    for i,d in enumerate(coef):
        if d==0:
            continue
        if markzero:
            markfir=i
        markzero=False
        fuhao=""
        xishu=""
        zhishu=""
        if d>0 and i!=markfir:
            fuhao="+"
        if d<0:
            fuhao= "-"
        if n-i-1==0:
            xishu= "%d" % (abs(d))
        elif n-i-1==1:
            if abs(d)!=1:
                xishu = "%d" % (abs(d))
            zhishu="x"
        else:
            if abs(d)!=1:
                xishu = "%d" % (abs(d))
            zhishu = "x^%d" % (n-i-1)
        res+=fuhao+xishu+zhishu
    if markzero:
        print "0"
    else:
        print res
coef=[1,-1,3,5,0]
poly(coef)

第四版,分为五种情况,简单明了,是最理想的方案

# your code goes here
def poly(coef):
    res = ""
    coef.reverse()
    n=len(coef)
    markzero=True
    markfir=0

    for i,d in enumerate(coef):
        if d==0:
            continue
        if markzero:
            markfir=i
        markzero=False
        fuhao=""
        zhishu=""
        if d>0 and i!=markfir:
            fuhao="+"
        if d<0:
            fuhao= "-"
        if n-i-1==0:
            zhishu="%d" % (abs(d))
        elif n-i-1==1 and abs(d)!=1:
            zhishu = "%dx" % (abs(d))
        elif n-i-1==1 and abs(d)==1:
            zhishu="x"
        elif abs(d)==1:
            zhishu="x^%d" % (n-i-1)
        else:
            zhishu = "%dx^%d" % (abs(d),n-i-1)
        res+=fuhao+zhishu
    if markzero:
        print "0"
    else:
        print res
coef=[1,-1,3,5,0]
poly(coef)

 C++版:

和python版大同小异,只不过多了些分号和大括号,还有代码多了些,需要注意的是C++中将整数转变为字符串,需要include<string>

,调用 to_string()方法

#include <iostream>
using namespace std;
#include<string>

int* reverse(int* list, int size){
    for(int i=0;i<size/2;i++){
        int tmp=list[i];
        list[i]=list[size-i-1];
        list[size-i-1]=tmp;
    }
    return list;
}
int main() {
    // your code goes here
    int coef[3]={1,11,0};
    int n= sizeof(coef)/sizeof(int);
    int* revcoef=reverse(coef,n);
    
    int firstnonzero=0;
    bool markallzero=true;
    string res="";
    for(int i=0;i<n;i++){
        if(coef[i]==0){
            continue;
        }
        if(markallzero){
            firstnonzero=i;
        }
        markallzero=false;
        
        if(coef[i]>0  && i!=firstnonzero){
            res+="+";
        }
        if(coef[i]<0){
            res+="-";
        }
        if(n-i-1==0){
            res+=to_string(abs(coef[i]));
        }else if(n-i-1==1 && abs(coef[i])==1){
            res+="x";
        }else if(n-i-1==1 && abs(coef[i])!=1){
            res+=to_string(abs(coef[i]))+"x";
        }else if(n-i-1!=1 && abs(coef[i])==1){
            res+="x^"+to_string(n-i-1);
        }else{
            res+=to_string(abs(coef[i]))+"x^"+to_string(n-i-1);
        }
    }
    if(markallzero){
        cout<<"0";
    }else{
        cout<<res;
    }
    return 0;
}

 

posted @ 2017-08-30 23:06  xqnq2007  阅读(404)  评论(0编辑  收藏  举报