package str; public class ArrangeNumber { /** * :用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) // 如果没要找到字符返回-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]++; } // 如果1出现1次,2出现2次,3,4,5各出现一次,返回true, if (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 1 && a[4] == 1) return true; else return false; } }