洛谷刷题
P8682 [蓝桥杯 2019 省 B] 等差数列
题目 P8682 [蓝桥杯 2019 省 B] 等差数列
题解
import java.util.Arrays;
import java.util.Scanner;
public class ArithmeticSequence {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
long[] array = new long[num];
for(int i=0;i<num;i++) {
array[i] = sc.nextLong();
}
Arrays.sort(array);
long hh = abs(array[1]-array[0]); //中间变量,计算等差数列的差
for(int i=1;i<num-1;i++) {
if(abs(array[i+1]-array[i])<hh) {
hh = abs(array[i+1]-array[i]);
}
}
if(hh == 0) {
System.out.println(array.length);
}else {
long result = (array[array.length-1]-array[0])/hh+1;
System.out.println(result);
}
}
private static long abs(long num) {
if(num>0) {
return num;
}else {
return num*(-1);
}
}
}
标签:2024,P8682,long,蓝桥,num,打卡,array,等差数列
From: https://blog.csdn.net/weixin_53387320/article/details/136916198