1041A--Heist

题目

There was an electronic store heist last night.
All keyboards which were in the store yesterday were numbered in ascending order from some integer number x. For example, if x=4 and there were 3 keyboards in the store, then the devices had indices 4, 5and 6, and if x=10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.
After the heist, only n keyboards remain, and they have indices a1,a2…,an.Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither x nor the number of keyboards in the store before the heist.

标题

The first line contains single integer n(1≤n≤1000) — the number of keyboards in the store that remained after the heist.

The second line contains n distinct integers a1,a2,…,an (1≤ai≤1000000000) — the indices of the remaining keyboards. The integers ai are given in arbitrary order and are pairwise distinct.

Output

Print the minimum possible number of keyboards that have been stolen if the staff remember neither x nor the number of keyboards in the store before the heist.

Examples

input
4
10 13 12 8
output
2
input
5
7 5 6 4 8
output
0

Note

In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist. In the second example, if x=4 then nothing was stolen during the heist.
题意:晚上,一家电子商店发生盗窃,店主不记得之前店里有多少键盘数量,但是键盘序号自然数序的,求其店里丢失键盘的最小数量。

解题思路:将键盘的序号按照从小到大排列,然后后一个键盘序号减去前一个键盘的序号,如果差值大于1,则丢失键盘数量为差值-1,最后累加丢失的键盘数量。

AC—Code

import java.util.Arrays;
import java.util.Scanner;

public class Heist {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext())
		{
			int n = sc.nextInt();
			int[] a = new int[n];
			int count = 0;
			for(int i = 0;i<n;i++)
			{
				a[i] = sc.nextInt();
			}
			Arrays.sort(a);
			for(int i=0;i<n-1;i++)
			{
				int ans = a[i+1]-a[i];
				if(ans>1)
				{
					count += ans-1;
				}
			}
			System.out.println(count);
		}
		sc.close();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值