题解 | #等差数列#

等差数列

https://www.nowcoder.com/practice/e11bc3a213d24fc1989b21a7c8b50c3f

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int [] array = new int [n];
        for(int i = 0;i<n;i++){
            array[i] = in.nextInt();
        }
        Arrays.sort(array);
        int d = array[1]-array[0];
        boolean judge = true;
        for(int i = 2;i<n;i++){
            if(array[i]-array[i-1]!=d)  {
                judge=false;
                break;
            }
        }
        if(judge)   System.out.println("Possible");
        else   System.out.println("Impossible");
    }
}

排序一下就可以。

全部评论

相关推荐

1jian10:48h没写面评会变成这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务