问题已说明here 。 我编写了代码来完成任务。
/**
* Created by aditya on 16-10-2014.
*/
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(new BufferedInputStream(System.in));
int n = scan.nextInt();
int k = scan.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>(k);
ArrayList<Integer> list_out = new ArrayList<Integer>(k);
for (int i = 0; i < k; i++) {
list.add(scan.nextInt());
}
for (int j = 0; j < k; j++) {
list_out.add(next_perm(list.get(j), n));
}
for (int temp : list_out) {
System.out.println(temp);
}
System.out.flush();
}
public static int next_perm(int k, int n) {
int perm = 0;
int number = k;
int num[] = new int[n];
for (int i = 0; i < n; i++) {
num[i] = number % 10;
number /= 10;
}
int k_max = 0;
for (int i = 0; i < n; i++) {
if (i == (n-1))
break;
if (num[i + 1] >= num[i]) {
++k_max;
} else if (num[i + 1] < num[i]) {
break;
}
}
if(k_max==(n-1))return k;
int j_max = k_max+1;
for (int i = k_max; i >= 0; i--) {
if (num[i] > num[k_max + 1]) {
--j_max;
} else {
break;
}
}
int temp=num[j_max];
num[j_max]=num[k_max+1];
num[k_max+1]=temp;
for(int i=k_max,j=0;i>j;i--,j++){
int tempo = num[i];
num[i]=num[j];
num[j]=tempo;
}
for (int i = 0; i < n; i++) {
perm += num[i] * (int) Math.pow(10, i);
}
return perm;
}
}
我手动运行了 n=1 到 4 的 1,2,..n 的所有排列并得到了正确的结果。对于那些想了解其逻辑的人,请参阅 here 。不显示任何警告或错误。当提交给在线评委时,它宣布它是错误的答案(在进行了 9 次测试之后)。 [请参阅下面的链接和结果] 9 个测试必须是与每个测试相对应的 n=1 到 9 的所有排列的子集,因此至少我的 4 个测试应该是正确的,但我的代码没有通过任何测试,请帮助我,我看看没有错误。
编辑:查看 n=4 here 情况下的示例输入输出.也在下面添加了
Note: I converted "\n" to "space" for space issues in question
(Input) 4 24 1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321
(Output) 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 4321
编辑:查看结果 here 。也添加到这里:
Problem: NEXTPERM
State: Wrong Answer
Total score for this submission: 0Test Case #0 for 2 points Wrong Answer Runtime: 0.12
Test Case #1 for 2 points Wrong Answer Runtime: 0.124
Test Case #2 for 2 points Wrong Answer Runtime: 0.124
Test Case #3 for 2 points Wrong Answer Runtime: 0.12
Test Case #4 for 2 points Wrong Answer Runtime: 0.116
Test Case #5 for 2 points Wrong Answer Runtime: 0.124
Test Case #6 for 2 points Wrong Answer Runtime: 0.124
Test Case #7 for 2 points Wrong Answer Runtime: 0.14
Test Case #8 for 2 points Wrong Answer Runtime: 0.108
Test Case #9 for 2 points Wrong Answer Runtime: 0.124
最佳答案
显然它适用于:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
/**
* Created by Aditya on 14-10-2014.
*/
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine());
ArrayList<String> lines = new ArrayList<String>();
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> words_2 = new ArrayList<String>();
boolean once_entered = true;
for (int i = 0; i < n; i++) {
lines.add(i, scan.nextLine() + " ");
}
for (int i = 0; i < n; i++) {
String word = "";
for (int j = 0; j < lines.get(i).length(); j++) {
char char_0 = lines.get(i).toLowerCase().charAt(j);
if ((int) (char_0) >= (int) ('a') && (int) (char_0) <= (int) ('z')) {
word += char_0;
once_entered = false;
} else if (!once_entered) {
words.add(word);
word = "";
once_entered = true;
}
}
}
for (int i = 0; i < words.size(); i++) {
boolean contains =false;
for(int j=0;j<words_2.size();j++){
if(words_2.get(j).contentEquals(words.get(i)))
contains=true;
}
if(!contains)
words_2.add(words.get(i));
}
Collections.sort(words_2);
System.out.println(words_2.size());
for (int i = 0; i < words_2.size(); i++) {
System.out.println(words_2.get(i));
}
}
}
似乎存在一些重复问题和一些小问题。
关于java - 返回数字的下一个排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26418241/