寒假每日一题打卡day20——AcWing 420. 火星人
作者:互联网
【题目描述】
AcWing 420. 火星人
最后一个答案错误,不知道为什么
import java.io.*;
class Main{
static int N = 10010;
public static void main(String args[])throws Exception{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
int f[] = new int[N];
int n = Integer.parseInt(bf.readLine());
int m = Integer.parseInt(bf.readLine());
String strArr[] = bf.readLine().split(" ");
for(int i = 1; i <= n; i++) f[i] = Integer.parseInt( strArr[i - 1] );
while(m > 0){
int k = n;
while(f[k] < f[k - 1]) k --; //逆序
int t = k; //找到第一个逆序的位置k - 1
while( f[ k - 1] < f[t + 1]) t ++; //找到[k, n]中第一个大于f[k - 1]的元素位置t
//交换f[k - 1] 和t
int tmp = f[ k - 1];
f[k - 1] = f[ t ];
f[t] = tmp;
//反转[k,n]区间
for(int i =k, j = n; i < j; i++, j--){
int c = f[k];
f[k] = f[j];
f[j] = c;
}
m --;
}
for(int i = 1; i <= n; i++ )System.out.print(f[i] + " ");
}
}
标签:bf,火星人,int,day20,--,new,打卡,readLine 来源: https://blog.csdn.net/weixin_44855907/article/details/113360834