其他分享
首页 > 其他分享> > LuoGu -- p1067

LuoGu -- p1067

作者:互联网

多项式输出

#include<stdio.h>
int n;
int a[105];

int main() {
    int flag = 0;
    scanf("%d", &n);
    for(int i = 0; i <=n; i++) {
        scanf("%d", &a[i]);
        if(a[i] == 0) continue;
        if(i==0){
            if(a[i]==1){
                printf("x^%d",n-i);
            }else if(a[i]==-1){
                printf("-x^%d",n-i);
            }else{
                printf("%dx^%d",a[i],n-i);
            }
            flag = 1;
        }else if(i==n){
            if(a[i]==1){
                printf("+1");
            }else if(a[i]==-1){
                printf("-1");
            }else{
                if(a[i]>0){
                    printf("+%d",a[i]);
                }else{
                    printf("%d",a[i]);
                }
            }
        }else{
            if(a[i]>0){
                if(a[i]==1){
                    if(!flag){
                        printf("x^%d",n-i);
                    }else{
                        printf("+x^%d",n-i);
                    }
                }else{
                    if(!flag){
                        printf("%dx^%d",a[i],n-i);
                    }else{
                        printf("%dx^%d",a[i],n-i);
                    }
                }    
            }else{
                if(a[i]==-1){
                    printf("-x^%d",n-i);
                }else{
                    printf("%dx^%d",a[i],n-i); 
                }
            }
        }
    }
    return 0;
}

 

标签:int,LuoGu,else,flag,dx,printf,main,p1067
来源: https://www.cnblogs.com/kyrie211/p/11108157.html