Question number: | 201409-1 |
Question name: | Adjacent pairs |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description Given n different integers, ask how many pairs of integers in these numbers, their values differ by exactly 1. Input format The first line of input contains an integer n, which represents the number of given integers. Output format Output an integer, representing the number of pairs whose values differ by exactly 1. Sample input 6 Sample output 3 Sample description Pairs whose values differ by exactly 1 include (2, 3), (6, 7), (7, 8). Evaluation use case scale and conventions 1<=n<=1000, the given integer is a non-negative integer not exceeding 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);
}
}