題目:
代碼:
import java.util.Scanner;
public class AdjacentPairs {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n=0;
int[] number;
n = scan.nextInt();
number = new int[n+1];
for(int i=1;i<=n;i++) {
number[i]=scan.nextInt();
}
System.out.println(ap(number));
}
public static int ap(int[] number) {
int count=0;
int n;
n=number.length-1;
for(int i=1;i<=n-1;i++)
for(int j=i+1;j<=n;j++) {
if(Math.abs(number[i]-number[j])==1)
count++;
}
return count;
}
}
主類名稱改為Main