import java.util.ArrayList;
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (sc.hasNext()) {
int n=sc.nextInt();
sc.nextLine();
String[] s=sc.nextLine().split(" ");
long[] arr=new long[n];
for (int i = 0; i < s.length; i++) {
arr[i]=Long.valueOf(s[i]);
}
int res = moveBicycle(n, arr);
System.out.println("移动车的数量:"+res);
}
}
/**
* @param n 车的数量
* @param m 车停放的位置
* @return 移动车的最少数量
*/
public static int moveBicycle(int n,long[] m) {
int count=0;//在一条直线上的点的数量
int gap=0;
//移动的索引
ArrayList<Integer> list = new ArrayList<Integer>();
//移动后的顺序
ArrayList<Long> list2 = new ArrayList<Long>();
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
//i,j为任意两个点的索引
int k =(int)(m[j]-m[i])/(j-i); //斜率,斜率是整数
//直线:y-m[i]=k*(x-i)
int tempCount=0;
for (int h = 0; h < n; h++) { //将n个数遍历一遍
if(m[h]-m[i]==k*(h-i)){
tempCount++;
}
}
if(tempCount > count){
count =tempCount ;
gap=k;
/*记录移动哪些车*/
list.clear();
list2.clear();
for (int h = 0; h < n; h++) {//将n个数遍历一遍
list2.add(m[h]);
if(m[h]-m[i]!=k*(h-i)){ //没有在一条直线上
list.add(h+1); //将数组中的索引都加1
list2.set(h,k*(h-i)+m[i]);
}
}
}
}
}
System.out.println("移动后每辆车间距为:"+ gap);
System.out.println("移动车的下标为(第1辆车记下标为1):"+ list);
System.out.println("移动后车的顺序为:");
System.out.println(list2);
return n-count;
}
}
//结果
4
1 3 6 7
移动后每辆车间距为:2
移动车的下标为(第1辆车记下标为1):[3]
移动后车的顺序为:
[1, 3, 5, 7]
移动车的数量:1
编程题:间隔相同
最新推荐文章于 2020-08-23 10:00:58 发布