题解 | #多项式输出#

多项式输出

https://ac.nowcoder.com/acm/problem/16622


import java.util.Scanner;
public class Main {
    public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        boolean flag = true;
        for(int i = 0;i<n-1;i++){
            int m = sc.nextInt();
            
            if(flag){
                if(m==1){
                    System.out.print("x^");
                    System.out.print(n-i);
                }
                else if(m==-1){
                    System.out.print("-x^");
                    System.out.print(n-i);
                }else if(m==0){
                    
                }else{
                    System.out.print(m);
                    System.out.print("x^");
                    System.out.print(n-i);
                }
            }else{
                if(m==-1){
                    System.out.print("-x^");
                    System.out.print(n-i);
                }
                else if(m==1){
                    System.out.print("+");
                    System.out.print("x^");
                    System.out.print(n-i);
                }else if(m==0){
                    
                }else if(m<0){
                    System.out.print(m);
                    System.out.print("x^");
                    System.out.print(n-i);
                }else if(m>0){
                    System.out.print("+");
                    System.out.print(m);
                    System.out.print("x^");
                    System.out.print(n-i);
                }
            }
            if(m!=0) flag = false;
        }

            int m= sc.nextInt();
            
            if(flag){
                if(m==1){
                    System.out.print("x");
                }
                else if(m==-1){
                    System.out.print("-x");
                }else if(m==0){
                    
                }else{
                    System.out.print(m);
                    System.out.print("x");
                }
            }else{
                if(m==-1){
                    System.out.print("-x");
                }
                else if(m==1){
                    System.out.print("+");
                    System.out.print("x");
                }else if(m==0){
                    
                }else if(m<0){
                    System.out.print(m);
                    System.out.print("x");
                }else if(m>0){
                    System.out.print("+");
                    System.out.print(m);
                    System.out.print("x");
                }
            }
            if(m!=0) flag = false;


        int end = sc.nextInt();
        if(flag){
            System.out.print(end);
        }else{
            if(end>0){
                System.out.print("+");
                System.out.print(end);
            }else if(end<0){
                System.out.print(end);
            }else{
                
            }
        }
    }
    
}
全部评论

相关推荐

小红书 后端开发 总包n+8w+期权
点赞 评论 收藏
分享
11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务