Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 返回数字的下一个排列_Java_Permutation - Fatal编程技术网

Java 返回数字的下一个排列

Java 返回数字的下一个排列,java,permutation,Java,Permutation,问题已经说明。 我写了一个代码来完成这项任务 /** * 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

问题已经说明。 我写了一个代码来完成这项任务

/**
 * 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的所有排列,得到了正确的结果。对于那些想了解逻辑的人,请参见。不会显示任何警告或错误。当提交给在线评委时,在进行了9次测试后,它宣布这是一个错误的答案。[请参见下面的链接和结果]9个测试必须是每个测试对应的n=1到9的所有排列的子集,因此至少我的4个测试应该是正确的,但我的代码没有通过任何测试,请帮助我,我看不到任何错误

编辑:参见示例输入输出,了解n=4的情况。下面也添加了 注意:我已将空间问题转换为空间

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

编辑:查看结果。这里也添加了: 问题:下一个术语 国家:回答错误 此提交的总分: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


显然,它与:

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

似乎有一些重复和一些次要的问题。

我认为这个问题本质上是使用同一组数字查找下一个较大的数字。看看这个:@Sinstein是的,但是对于大量数字的情况,这会很耗时,你只需要处理最多10个数字,你只需要找到下一个排列,而不是第j个排列。从右侧开始扫描,对于遇到的每一个数字,检查是否存在一个比前一组数字的值小的数字,如果找到一个,则交换2,否则转到下一个数字。这给出了一个关于^2的命令,对于n=1000是不错的。@Sinstein不是很粗鲁,你认为呢,对于n=10,它会在3秒钟内完成吗?我很肯定它会的。此外,提供的链接中的答案在上完成了该任务。