返回一个数字的下一个排列

时间:2014-10-17 05:08:35

标签: java permutation

问题是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;
    }
}

我手动运行1,2,... n的所有排列,n = 1到4,得到了正确的结果。对于那些想知道逻辑的人,请参阅here。不显示任何警告或错误。当提交给在线评委时,它宣布错误答案(在进行9次测试后)。 [见下面的链接和结果] 9个测试必须是对应于每个测试的n = 1到9的所有排列的子集,所以至少我的4个测试应该是正确的,但我的代码没有通过测试,请帮助我,我看到没有错误。

编辑:查看n = 4 here案例的示例输入输出。以下也添加了

  

注意:我将“\ n”转换为“空格”以查找有问题的空间问题

     

(输入)   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

     

(输出)   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。这里也添加了:

  

问题:NEXTPERM
  国家:错误的答案
  此提交的总分:0

     

测试案例#0获得2分   错误的答案   运行时间:0.12

     

测试案例#1获得2分   错误的答案   运行时间:0.124

     

测试案例#2获得2分   错误的答案   运行时间:0.124

     

测试案例#3获得2分   错误的答案   运行时间:0.12

     

测试案例#4获得2分   错误的答案   运行时间:0.116

     

测试案例#5获得2分   错误的答案   运行时间:0.124

     

测试案例#6获得2分   错误的答案   运行时间:0.124

     

测试用例#7获得2分   错误的答案   运行时间:0.14

     

测试案例#8获得2分   错误的答案   运行时间:0.108

     

测试案例#9获得2分   错误的答案   运行时间:0.124

1 个答案:

答案 0 :(得分:0)

显然它适用于:

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));
        }
    }
}

似乎重复和一些小问题存在一些问题。