P1152 欢乐的跳---Java

题目描述

一个nnn个元素的整数数组,如果数组两个连续元素之间差的绝对值包括了[1,n−1][1,n-1][1,n−1]之间的所有整数,则称之符合“欢乐的跳”,如数组14231 4 2 31423符合“欢乐的跳”,因为差的绝对值分别为:3,2,13,2,13,2,1。

给定一个数组,你的任务是判断该数组是否符合“欢乐的跳”。
输入格式

每组测试数据第一行以一个整数n(1≤n≤1000)n(1 \le n \le 1000)n(1≤n≤1000)开始,接下来nnn个空格隔开的在[−108-10^8−108,10810^8108]之间的整数。
输出格式

对于每组测试数据,输出一行若该数组符合“欢乐的跳”则输出"Jolly",否则输出"Not jolly"。
输入输出样例
输入 #1

4 1 4 2 3

输出 #1

Jolly

输入 #2

5 1 4 2 -1 6

输出 #2

Not jolly

说明/提示

1≤n≤10001 \le n \le 10001≤n≤1000

import java.util.*;

public class P1152 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] num = new int[n];
        for (int i = 0; i < n; i++) {
            num[i] = sc.nextInt();
        }
        ArrayList list = new ArrayList();
        for (int i = 0; i < n-1; i++) {
            for (int j = i + 1; j <= i+1; j++) {
                int tmp = num[i] - num[j];
                list.add(Math.abs(tmp));
            }
        }
        Set<Integer> set = new HashSet<>(list);
        list.clear();
        list.addAll(set);
        ArrayList arrayList = new ArrayList();
        for (int i = 1; i <= n - 1; i++) {
            arrayList.add(i);
        }
        Collections.sort(list);
        if (list.get(0).equals(0)) {
            list.remove(0);
        }
        int count = 0;
        if (list.size() > arrayList.size()||list.size()==arrayList.size()) {
            for (int i = 0; i < arrayList.size(); i++) {
                if (list.get(i).equals(arrayList.get(i)))
                    count++;
            }
            if (count == arrayList.size()) {
                System.out.print("Jolly");
            }
        } else {
            System.out.print("Not jolly");
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值