将它转化成java语言#include<bits/stdc++.h> using namespace std; int p[10005]; int main() { int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ scanf("%d",&p[i]); } int cnt=0; do{ if(cnt==m){ for(int i=0;i<n;i++){ if(i!=n-1) printf("%d ",p[i]); else printf("%d\n",p[i]); } break; } cnt++; }while(next_permutation(p,p+n)); return 0; }
时间: 2024-03-31 16:34:16 浏览: 83
万能头文件#include“bitsstdc++.h”.pdf
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] p = new int[10005];
int n = sc.nextInt();
int m = sc.nextInt();
for(int i=0;i<n;i++){
p[i] = sc.nextInt();
}
int cnt=0;
do{
if(cnt==m){
for(int i=0;i<n;i++){
if(i!=n-1)
System.out.print(p[i]+" ");
else
System.out.println(p[i]);
}
break;
}
cnt++;
}while(next_permutation(p,n));
}
public static boolean next_permutation(int[] p, int n){
int i = n-2;
while(i>=0 && p[i]>=p[i+1]){
i--;
}
if(i<0){
return false;
}
int j=n-1;
while(j>i && p[j]<=p[i]){
j--;
}
swap(p,i,j);
reverse(p,i+1,n-1);
return true;
}
public static void swap(int[] p, int i, int j){
int temp = p[i];
p[i] = p[j];
p[j] = temp;
}
public static void reverse(int[] p, int i, int j){
while(i<j){
swap(p,i,j);
i++;
j--;
}
}
}
阅读全文