44 lines
981 B
Java
44 lines
981 B
Java
package LanQiaoOJ;
|
||
|
||
import java.util.Arrays;
|
||
import java.util.Scanner;
|
||
|
||
/*
|
||
* 题目描述
|
||
数学老师给小明出了一道等差数列求和的题目。但
|
||
是粗心的小明忘记了一部分的数列,只记得其中N个整数。现在给出这N个整数,小明想知道包含这N个整
|
||
数的最短的等差数列有几项?
|
||
输入描述
|
||
输入的第一行包含一个整数N。第二行包含N个整数A1,A2,··,AN。(注意A1~AN并不一定是按等差数列中的顺序给出)
|
||
其中,2≤N≤10^5,0≤A:≤10^9。
|
||
输出描述
|
||
输出一个整数表示答案。*/
|
||
public class 等差数列 {
|
||
|
||
public static void main(String[] args) {
|
||
Scanner sc=new Scanner(System.in);
|
||
int n=sc.nextInt();
|
||
int []a=new int [n];
|
||
for(int i=0;i<n;i++) {
|
||
a[i]=sc.nextInt();
|
||
}
|
||
Arrays.sort(a);
|
||
//最小公差
|
||
int d=a[1]-a[0];
|
||
for(int i=1;i<n;i++) {
|
||
d=gcd(d,a[i]-a[i-1]);
|
||
}
|
||
//判断公差为0的情况
|
||
if(d==0)
|
||
System.out.print(n);
|
||
else
|
||
//计算项数
|
||
System.out.print((a[n-1]-a[0])/d+1);
|
||
}
|
||
//判断最大的公差
|
||
static int gcd(int a, int b) {
|
||
return b==0?a:gcd(b,a%b);
|
||
}
|
||
|
||
}
|