题目
Description
传说fans是一个数学天才。在他五岁那年,从一堆数字卡片中选出了4张 卡片:5,7,6,8。这4个数字有什么神秘之处呢?如果把这4张卡片自左往右的排成:5,6,7,8。你就会发现:原来这4个数字构成了等差数列!当年 fans选出了n组卡片,据说都能够构成等差数列。但是事实真的是这样吗?fans真的有这么神奇吗? n组数据就是fans选出的n组卡片,请你判断每一组卡片是否能构成等差数列.
Input
第一个数为数据的组数n,表示后面有n行,每行中的第一个数为该组数据的元素个数m(1≤m≤100),其后是m个正整数(不会超出int的表示范围)。
Output
如果能够构成等差数列,输出“yes”,否则输出“no”。
Sample Input
2
4 5 7 6 8
8 1 7 3 2 8 12 78 3
Sample Output
yes
no
代码块
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cn = new Scanner(System.in);
int n = cn.nextInt();
while (n-- > 0) {
int m = cn.nextInt();
int[] a = new int[m];
for(int i =0;i<m;i++){
a[i] = cn.nextInt();
}
Arrays.sort(a);//进行排序
int lis = a[0]-a[1];//算出第一个数与第二个数的差值
boolean flag = true;
for(int i=0;i<m-1;i++){//进行判断是否为公差
if(a[i]-a[i+1]!=lis) {
flag = false;
break;
}
}
if(flag) System.out.println("yes");
else System.out.println("no");
}
}
}