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

来源:互联网 发布:json时间格式转换 编辑:程序博客网 时间:2024/04/30 09:48
 关键字:用1、2、2、3、4、5这六个数字,数字排序经典算法


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;
}
}
原创粉丝点击