试题编号: | 201409-1 |
试题名称: | 相邻数对 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1。 输入格式 输入的第一行包含一个整数n,表示给定整数的个数。 输出格式 输出一个整数,表示值正好相差1的数对的个数。 样例输入 6 样例输出 3 样例说明
扫描二维码关注公众号,回复:
11674957 查看本文章
值正好相差1的数对包括(2, 3), (6, 7), (7, 8)。 评测用例规模与约定 1<=n<=1000,给定的整数为不超过10000的非负整数。 |
import java.util.Scanner;
public class 相邻的数201409_1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int len=sc.nextInt();
int[] a=new int[10001];
for(int i=1;i<=len;i++){
a[sc.nextInt()]+=1;
}
int sum=0;
for(int i=1;i<10000;i++){
if(a[i+1]>a[i]){
sum+=(a[i]);
}else{
sum+=(a[i+1]);
}
}
System.out.println(sum);
}
}