用1、2、2、3、4、5这六个数字,数字排序经典算法

xiaoxiao2026-09-11  1

public class Numarray { /** 用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等,要求: "4 "不能在第三位, "3 "与 "5 "不能相连. */ public static void main(String[] a){ long start; System.out.println("结果是:"); int count = 0 ; for(start=122345;start <=543221;start++){ String s = String.valueOf(start); if(Validate(s)) { if((s.indexOf("35")==-1)&&(s.indexOf("53")==-1)&&(s.charAt(2)!='4')){ System.out.println(s); count++; } } } System.out.println("最后结果共"+count); } public static boolean Validate(String l) { int[] a = new int[]{0,0,0,0,0}; for(int i=0;i <6;i++) { if(l.charAt(i)=='1') a[0]++; if(l.charAt(i)=='2') a[1]++; if(l.charAt(i)=='3') a[2]++; if(l.charAt(i)=='4') a[3]++; if(l.charAt(i)=='5') a[4]++; } if(a[0]==1&&a[1]==2&&a[2]==1&&a[3]==1&&a[4]==1) return true; else return false; } }
转载请注明原文地址: https://www.6miu.com/read-5052537.html

最新回复(0)