forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxiaoma_01.java
More file actions
80 lines (69 loc) · 2.12 KB
/
xiaoma_01.java
File metadata and controls
80 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package BiShi;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* @Classname xiaoma_01
* @Description 交换数组中相邻数字的值,使得最后输出的数字最大
* @Date 19-4-25 下午8:10
* @Created by mao<[email protected]>
*/
public class xiaoma_01 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("/home/mao/workspace/java/src/BiShi/test4"));
//组数
int count=sc.nextInt();
ArrayList<Integer> nums=new ArrayList<>();
for(int i=0;i<count;i++){
//交换次数
int b=sc.nextInt();
//元素个数
int a=sc.nextInt();
nums.clear();
for(int j=0;j<a;j++){
nums.add(sc.nextInt());
}
// 交换b次
while(b>0){
for(int k=0;k<a;k++){
//找到下标
int c=findmax(nums,k+1,b);
if(nums.get(k)>nums.get(c)){
continue;
}else{
//挪动
move(nums,k,c);
//挪动次数
int m= c-k;
b=b-m;
break;
}
}
}
int m=0;
for(m=0;m<nums.size()-1;m++){
System.out.print(nums.get(m)+" ");
}
System.out.println(nums.get(m));
}
}
private static void move(ArrayList<Integer> array,int start, int end){
int tmp=array.get(end);
for(int i=end-1;i>=start;i--){
array.set(i+1,array.get(i));
}
array.set(start,tmp);
}
private static int findmax(ArrayList<Integer> array,int start, int len){
int maxIndex=start;
int max=array.get(start);
for(int i=start;i<array.size()&&i<start+len;i++){
if(max<array.get(i)){
max=array.get(i);
maxIndex=i;
}
}
return maxIndex;
}
}