Java算法_next_permutation()——HDU 1027:Ignatius and the Princess II

next_permutation()

c++ STL中的next_permutation方法,求一下个字典序.

java实现过程

  1. 从后往前找第一个后一项大于前一项的数num[i]>num[i-1]
  2. 以i-1 为基准,从后往前找第一个大于num[i-1]的数,记做num[j],然后交换这两个数
  3. 将 i-1 后面的数全部翻转
import java.util.*;
public class Main {
	public static void reverse(int a,int b,int[] num) {
		while(a<b) {
			int t=num[a]; num[a]=num[b] ; num[b]=t ; 
			++a ; --b ; 
		}
	}
	public static boolean next_permutation(int a,int b,int[] num) {
		int i=b-1 ; 
		while(i>a&&num[i]<=num[i-1])	--i ;	//从后往前找第一个后一项大于前一项的 
		if (i==a)	return false ; 	//到头说明整个序列为逆序,没有下一个字典序了
		--i ;
		int j=b-1 ; 
		while(j>i&&num[j]<=num[i])	--j ; //从后往前找第一个大于num[i]的数
		int t=num[i]; num[i]=num[j]; num[j]=t ; //交换两个数
		reverse(i+1,b-1,num) ;	//i后面的数翻转
		return true ; 
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in) ; 
		int[] num = {1,2,3,4} ; 
		do {
			for(int i=0 ; i<num.length ; ++i)	System.out.print(num[i]+" ");
			System.out.println();
		}while(next_permutation(0,num.length,num)) ; 
	
	}
	
}

HDU 1027:Ignatius and the Princess II

http://acm.hdu.edu.cn/showproblem.php?pid=1027

Problem Description

Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, “I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too.” Ignatius says confidently, “OK, at last, I will save the Princess.”

现在我们的英雄找到了魔王风5166的门。他打开门,发现feng5166即将杀死我们美丽的公主。但现在魔王必须先打败我们的英雄。奉5166说:“我有三个问题要问你,如果你能解决,我就放了公主,否则你也会成为我的晚餐。”伊格内修斯自信地说:“好吧,最后,我会救公主的。”

“Now I will show you the first problem.” feng5166 says, “Given a sequence of number 1 to N, we define that 1,2,3…N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it’s easy to see the second smallest sequence is 1,2,3…N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It’s easy, isn’t is? Hahahahaha…”
Can you help Ignatius to solve this problem?

“现在我要给你们看第一个问题,”feng5166说,“给定一个从1到N的序列,我们定义1、2、3……N-1,N是所有可以由数字1到N组成的序列中最小的序列(在这个问题中,每个数字可以且应该只使用一次)。很容易看出第二小的数列是1 2 3…N N-1。现在我给你两个数,N和m,你应该告诉我第m个最小的数列是由1到N组成的,这很简单,不是吗?Hahahahaha……”
你能帮伊格那丢解决这个问题吗?

Input

The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub’s demand. The input is terminated by the end of file.

输入包含几个测试用例。每个测试用例由两个数字组成,N和M(1<=N<=1000, 1<=M<=10000)。你可以假设总有一个序列满足魔王的要求。输入以文件结尾结束。

Output

For each test case, you only have to output the sequence satisfied the BEelzebub’s demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.

对于每个测试用例,您只需要输出满足BEelzebub需求的序列。当输出一个序列时,应该在两个数字之间输出一个空格,但不要在最后一个数字之后输出任何空格。

Sample Input

6 4
11 8

Sample Output

1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
package com.hhhyixin.book.cp03.HDU_1027;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        new Main().funcation();
    }

    void funcation() {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int N = scanner.nextInt();
            int M = scanner.nextInt();
            int[] num = new int[N];
            int sum = 1;
            for (int i = 1; i <= N; i++) {
                num[i - 1] = i;
            }
            do {
                if (sum == M) {
                    for (int i = 0; i < num.length; ++i) System.out.print(num[i] + " ");
                    System.out.println();
                }
                sum++;
            } while (next_permutation(0, num.length, num));
        }
    }

    boolean next_permutation(int a, int b, int[] num) {
        int i = b - 1;
        while (i > a && num[i] <= num[i - 1]) --i;    //从后往前找第一个后一项大于前一项的
        if (i == a) return false;    //到头说明整个序列为逆序,没有下一个字典序了
        --i;
        int j = b - 1;
        while (j > i && num[j] <= num[i]) --j; //从后往前找第一个大于num[i]的数
        int t = num[i];
        num[i] = num[j];
        num[j] = t; //交换两个数
        reverse(i + 1, b - 1, num);    //i后面的数翻转
        return true;
    }

    void reverse(int a, int b, int[] num) {
        while (a < b) {
            int t = num[a];
            num[a] = num[b];
            num[b] = t;
            ++a;
            --b;
        }
    }
}

image-20221009132420743

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

要什么自行车儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值