当前位置: 代码迷 >> Java相关 >> 怎样打印出所以5个字符的组合()
  详细解决方案

怎样打印出所以5个字符的组合()

热度:89   发布时间:2016-04-22 20:55:29.0
怎样打印出所以5个字符的组合(在线等)
本帖最后由 changjiadi 于 2014-10-30 11:41:21 编辑
根据以下条件写一段代码,打印出所有5个字符的可能组合:
1. 至少有一个大写字母
2. 至少有一个小写字母
3. 至少有一个数字
4.两个一样的数或者字母不能连在一起(比如 Haxa5是可以的,Haax5就不可以)

我大概想到的是用循环的for来写,但是是真的不知道怎么写-_-
谢谢了!
------解决思路----------------------
1、定义五个字符型变量c1,c2,c3,c4,c5
2、五层循环遍历,得到五个字符
        C1遍历0-9 c2遍历a-z c3遍历A-Z C4 C5遍历0-9a-zA-Z所有数字字母
         (C4>=C1/C2/C3,C5>=C4避免重复
        举个例子来说,如果C1当前取值为3,遍历c4时就是遍历3-9就可以了
3、得到这5个字符的排列组合,去除临近重复的数据

感觉你这个全打印出来数据量实在是够大,不知道这到底是干啥用啊?越看越像作业题,没啥实际意义
------解决思路----------------------

    String[] str1 =
        {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z"};
    String[] str2 =
        {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
            "s", "t", "u", "v", "w", "x", "y", "z"};
    String[] str3 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
    Queue<String> queue = new LinkedList<String>();
    Random rand = new Random();
    int index = rand.nextInt(str1.length - 1);
    queue.add(str1[index]);
    index = rand.nextInt(str2.length - 1);
    queue.add(str2[index]);
    index = rand.nextInt(str3.length - 1);
    queue.add(str3[index]);
    while (true) {
      String str = null;
      switch(rand.nextInt(2)){
        case 0 : index = rand.nextInt(str1.length - 1);str = str1[index];break;
        case 1 : index = rand.nextInt(str2.length - 1);str = str2[index];break;
        case 2 : index = rand.nextInt(str3.length - 1);str = str3[index];break;
      }
      String lastStr = queue.peek();
      if(lastStr != null && str != null){
        if(!lastStr.equals(str)){
          queue.add(str);
          if(queue.size() == 5){
            break;
          }
        }
      }
    }
    System.out.println(queue);

两个一样的数或者字母不能连在一起
不知道包不包括大小写,例如Hh,如果不包括,参考上面的
------解决思路----------------------
import java.util.ArrayList;
import java.util.List;


public class Five {

public static void getFive(){
ArrayList li=new ArrayList();
for (int i =48; i<=57; i++) {
li.add((char)i);
}
for (int i =65; i<=90; i++) {
li.add((char)i);
}
for (int i =97; i<=122; i++) {
li.add((char)i);
}
System.out.println();
int a=li.size();
for (int h = 0; h <a; h++) {
               for(int i=0;i<a;i++){
                for(int j=0;j<a;j++){
                for(int k=0;k<a;k++){
                for(int l=0;l<a;l++){
                String s=""+li.get(h)+li.get(i)+li.get(j)+
               li.get(k)+li.get(l);
                if(judge(s,0)&&judge(s,2)&&judge(s,1)){
                for(int ss=0;ss<s.length()-1;ss++){
                if(s.charAt(ss)==s.charAt(ss+1)){
               break;
                
                
                }
                
               System.out.println(s+"--"); 
                }}}}}}}}
public  static boolean judge(String s,int b){
 switch (b) {
case 0: for(int i=0;i<s.length();i++){
if(s.charAt(i)>=48&&s.charAt(i)<=57) return true;
else if(i>=s.length())return false;

}
break;
case 2: for(int i=0;i<s.length();i++){
if(s.charAt(i)>=65&&s.charAt(i)<=90) return true;
else if(i>=s.length())return false;
}
break;
case 1:for(int i=0;i<s.length();i++){
if(s.charAt(i)>=97&&s.charAt(i)<=122) return true;
else if(i>=s.length())return false;
}
break;
default: return false;
}
return false;

}

public static void main(String[] args) {

getFive();
}





}
  相关解决方案