Java 排列组合_c语言排列组合函数

Java 排列组合_c语言排列组合函数importjava.util.Arrays;//利用二进制算法进行全排列//count1:170187//count2:291656publicclasstest{publicstaticvoidmain(String[]args){longstart=System.currentTimeMillis();count2();longend=System.currentTim…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

import java.util.Arrays;

//利用二进制算法进行全排列

//count1:170187

//count2:291656

public class test {

public static void main(String[] args) {

long start=System.currentTimeMillis();

count2();

long end=System.currentTimeMillis();

System.out.println(end-start);

}

private static void count2(){

int[] num=new int []{1,2,3,4,5,6,7,8,9};

for(int i=1;i

String str=Integer.toString(i,9);

int sz=str.length();

for(int j=0;j<9-sz;j++){

str=”0″+str;

}

char[] temp=str.toCharArray();

Arrays.sort(temp);

String gl=new String(temp);

if(!gl.equals(“012345678”)){

continue;

}

String result=””;

for(int m=0;m

result+=num[Integer.parseInt(str.charAt(m)+””)];

}

System.out.println(result);

}

}

public static void count1(){

int[] num=new int []{1,2,3,4,5,6,7,8,9};

int[] ss=new int []{0,1,2,3,4,5,6,7,8};

int[] temp=new int[9];

while(temp[0]<9){

temp[temp.length-1]++;

for(int i=temp.length-1;i>0;i–){

if(temp[i]==9){

temp[i]=0;

temp[i-1]++;

}

}

int []tt=temp.clone();

Arrays.sort(tt);

if(!Arrays.equals(tt,ss)){

continue;

}

String result=””;

for(int i=0;i

result+=num[temp[i]];

}

System.out.println(result);

}

}

}

一.利用二进制状态法求排列组合,此种方法比较容易懂,但是运行效率不高,小数据排列组合可以使用

二.用递归的思想来求排列跟组合,代码量比较大

package practice;

import java.util.ArrayList;

import java.util.List;public classTest1 {/**

* @param args*/

public static voidmain(String[] args) {//TODO Auto-generated method stub

Object[] tmp={1,2,3,4,5,6};//ArrayList rs=RandomC(tmp);

ArrayList rs=cmn(tmp,3);for(int i=0;i

{//System.out.print(i+”=”);

for(int j=0;j

{

System.out.print(rs.get(i)[j]+”,”);

}

System.out.println();

}

}//求一个数组的任意组合

static ArrayListRandomC(Object[] source)

{

ArrayList result=new ArrayList();if(source.length==1)

{

result.add(source);

}else{

Object[] psource=new Object[source.length-1];for(int i=0;i

{

psource[i]=source[i];

}

result=RandomC(psource);int len=result.size();//fn组合的长度

result.add((new Object[]{source[source.length-1]}));for(int i=0;i

{

Object[] tmp=new Object[result.get(i).length+1];for(int j=0;j

{

tmp[j]=result.get(i)[j];

}

tmp[tmp.length-1]=source[source.length-1];

result.add(tmp);

}

}returnresult;

}static ArrayList cmn(Object[] source,intn)

{

ArrayList result=new ArrayList();if(n==1)

{for(int i=0;i

{

result.add(newObject[]{source[i]});

}

}else if(source.length==n)

{

result.add(source);

}else{

Object[] psource=new Object[source.length-1];for(int i=0;i

{

psource[i]=source[i];

}

result=cmn(psource,n);

ArrayList tmp=cmn(psource,n-1);for(int i=0;i

{

Object[] rs=newObject[n];for(int j=0;j

{

rs[j]=tmp.get(i)[j];

}

rs[n-1]=source[source.length-1];

result.add(rs);

}

}returnresult;

}

}

三.利用动态规划的思想求排列和组合

packageAcm;//强大的求组合数

public classMainApp {public static voidmain(String[] args) {int[] num=new int[]{1,2,3,4,5};

String str=””;//求3个数的组合个数//count(0,str,num,3);//求1-n个数的组合个数

count1(0,str,num);

}private static void count1(int i, String str, int[] num) {if(i==num.length){

System.out.println(str);return;

}

count1(i+1,str,num);

count1(i+1,str+num[i]+”,”,num);

}private static void count(int i, String str, int[] num,intn) {if(n==0){

System.out.println(str);return;

}if(i==num.length){return;

}

count(i+1,str+num[i]+”,”,num,n-1);

count(i+1,str,num,n);

}

}

下面是求排列

packageAcm;//求排列,求各种排列或组合后排列

importjava.util.Arrays;importjava.util.Scanner;public classDemo19 {private static booleanf[];public static voidmain(String[] args) {

Scanner sc=newScanner(System.in);int sz=sc.nextInt();for(int i=0;i

f=new boolean[sum];

Arrays.fill(f,true);int[] num=new int[sum];for(int j=0;j

num[j]=j+1;

}int nn=sc.nextInt();

String str=””;

count(num,str,nn);

}

}/***

*@paramnum 表示要排列的数组

*@paramstr 以排列好的字符串

*@paramnn 剩下需要排列的个数,如果需要全排列,则nn为数组长度*/

private static void count(int[] num, String str, intnn) {if(nn==0){

System.out.println(str);return;

}for(int i=0;i

}

f[i]=false;

count(num,str+num[i],nn-1);

f[i]=true;

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/179096.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql报错日志文件在哪_windows硬件报错日志

    mysql报错日志文件在哪_windows硬件报错日志1.cmd打开服务,找到mysql服务,查看属性中my.ini配置文件所在位置:2.打开my.ini配置文件,查看datadir配置:datadir=C:/ProgramData/MySQL/MySQLServer5.7/Data此目录存储了错误日志文件3.查看错误日志文件名SHOWVARIABLESLIKE”%error%”;…

    2022年10月13日
    0
  • 解决方案:VS2017 无法打开源文件 stdio.h main.h 等头文件[通俗易懂]

    解决方案:VS2017 无法打开源文件 stdio.h main.h 等头文件[通俗易懂]问题描述:在VS2017中运行解决方案是有错误:“E1696 无法打开源文件“stdio.h” ”…原因:这种问题一般发生在该项目代码是在网上下载而来的情况,或者电脑重装新的系统等情况,导致电脑系统与该项目生成时所采用的windowsSDK不同,从而在默认的位置(已发生变化)找不到许多源文件。解决方案:1.在C++项目处(示例为“Fibonacci”),鼠标右击,弹出的菜…

    2022年6月16日
    79
  • 基于opencv的图像校正_伽马校正怎么设置

    基于opencv的图像校正_伽马校正怎么设置#include"stdafx.h"#include&lt;cmath&gt;#include&lt;iostream&gt;#include&lt;opencv2\core\core.hpp&gt;#include&lt;opencv2\highgui\highgui.hpp&gt;#include&lt;opencv2\imgproc\imgproc.hp…

    2022年9月25日
    0
  • 数组转LIst的几种方法

    数组转LIst的几种方法第一种方式/***针对数组类型转换*分别是int[]、long[]、double[],其他数据类型比如short[]、byte[]、char[],在JDK1.8中暂不支持*/List<Integer>intList=Arrays.stream(newint[]{1,2,3,}).boxed().collect(Collectors.toList());List<Long>longList=Arrays.stream(newlong[]

    2022年6月21日
    27
  • Pycharm全局搜索关键字[通俗易懂]

    Pycharm全局搜索关键字[通俗易懂]PythonTips

    2022年5月6日
    154
  • IDEA炫酷主题推荐!非常好看![通俗易懂]

    IDEA炫酷主题推荐!非常好看![通俗易懂]IntelliJIDEA介绍IDEA,全称IntelliJIDEA,是Java语言的集成开发环境,IDEA在业界被公认为是最好的java开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE支持、Ant、JUnit、CVS整合、代码审查、创新的GUI设计等方面的功能可以说是超常的。*IDEA的官网下载地址:IDEA下载地址平时开发中由…

    2022年5月31日
    87

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号