Java 欧拉工程 第二十四篇【0,1,2,3,4,5,6,7,8,9的第100万个字典排列是什么】

本文介绍了一种通过数学计算寻找特定字典序排列的方法。利用阶乘和递归原理,逐步确定目标序列中的每个数字位置,最终求得0-9数字的第100万个字典排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:排列是一个物体的有序安排。例如3124是1,2,3,4的一种排列。如果所有的排列按照数值或者字母序排序,我们称其为一个字典序。0,1,2的字典排列有:

012   021   102   120   201   210

0, 1, 2, 3, 4, 5, 6, 7, 8,9的第100万个字典排列是什么?

原题:

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012   021   102   120   201   210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

解题思路:由题意,3位数的字典排列共有6种,不难看出n位数的字典排列应有n!个,由字典排序的规律可知,0-9的排列除去第一位数,剩下的排列数量可表示位9! 9!=362880,所以我们要求的第 1 000 000数列应在第2个9!和第3个9!之间,即可以确定第一位数一定为2,而1 000 000-9!-9!=274240,除去确定的第一位数2,剩下的9位数的第274240个排列为我们所求,同理274240在第6个8!和第7个8!之间,舍弃了2之后, 0,1,3,4,5,6,7所以第二位数一定为7,依次类推,换算成公式可表示为

 m+(a)n!>1000000(m>=0,a>=0,n<-[0,9]),a即是我们需确定的值的位置,需注意在一位数确认时,在之后的排序中需要舍去。如下贴上java的代码:

public class Launcher {    
	    public static void main(String[] args) {  
	    	Vector<Integer> list= new Vector<Integer>();
	    	Vector<Integer> numList= new Vector<Integer>();
	    	int sum=0;
	    	for(int i=0;i<10;i++){
	    		list.add(i);
	    	}
	    	for(int j=9;j>=0;j--){
	    		for(int n=1;n<12;n++){
	    			if(sum+n*jiecheng(j)>1000000){
	    				numList.add(list.get(n-1));
	    				list.remove(n-1);
	    				sum+=(n-1)*jiecheng(j);
	    				break;
	    			}
	    		}
	    	}

	        System.out.println(numList);  
	    }
	    	        

	    public static int jiecheng(int n){
	    	int sum=1;
	    	for(int i=1;i<=n;i++){
	    		sum*=i;
	    	}
			return sum;
	    	
	    }
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值