CCF——相邻数对201409-1

问题描述
  给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1。
输入格式
  输入的第一行包含一个整数n,表示给定整数的个数。
  第二行包含所给定的n个整数。
输出格式
  输出一个整数,表示值正好相差1的数对的个数。
样例输入
6
10 2 6 3 7 8
样例输出
3
样例说明
  值正好相差1的数对包括(2, 3), (6, 7), (7, 8)。
评测用例规模与约定
  1<=n<=1000,给定的整数为不超过10000的非负整数。
 1 package com.ezreal;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // TODO Auto-generated method stub
12         new Main().run();
13     }
14     public void run(){
15             Scanner sc=new Scanner(System.in);  
16             int N=sc.nextInt();  
17             int[] a=new int[1000];  
18             int result=0;  
19             for(int i=0;i<N;i++){  
20                 a[i]=sc.nextInt();  
21             }  
22             for(int i=0;i<N;i++){  
23                 for(int j=i+1;j<N;j++){  
24                     if(Math.abs(a[i]-a[j])==1){  
25                         result+=1;  
26                     }  
27                 }  
28             }  
29             System.out.println(result);  
30     }
31 
32 }
原文地址:https://www.cnblogs.com/ezreal2016/p/5781905.html