面试题目记录
来源:互联网 发布:试衣间软件 编辑:程序博客网 时间:2024/06/03 01:16
用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
方法一:
public class Test3{ public static void main(String[] args) { int number = 0; int count = 0; String numberStr; for (int a = 1; a <= 5; a++) { for (int b = 1; b <= 5; b++) { for (int c = 1; c <= 5; c++) { for (int d = 1; d <= 5; d++) { for (int e = 1; e <= 5; e++) { // 打印出所有不重复的数字 if (a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e && d != e) { number = a * 10000 + b * 1000 + c * 100 + d * 10 + e; numberStr = String.valueOf(number); // 4不能再第3位, "3"与"5"不能相连 if (numberStr.indexOf("4") != 2 && numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1) { count++; System.out.println(numberStr); } } } } } } } System.out.println("count=" + count); }}
方法二:
public class Test3{ public static void main(String[] args) { int number = 0; int count = 0; String numberStr; for (int a = 1; a <= 5; a++) { for (int b = 1; b <= 5; b++) { int c=1; while(c<=5){ for (int d = 1; d <= 5; d++) { for (int e = 1; e <= 5; e++) { // 打印出所有不重复的数字 if (a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e && d != e) { number = a * 10000 + b * 1000 + c * 100 + d * 10 + e; numberStr = String.valueOf(number); // 4不能再第3位, "3"与"5"不能相连 if (numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1) { count++; System.out.println(numberStr); } } } } c++; if(c==4) c++; } } } System.out.println("count=" + count); }
方法三:
package com.lh.test;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Demo{public static void main(String[] args){ long start = System.currentTimeMillis();Test1();long end = System.currentTimeMillis();System.out.println("你的运行时间:" + (long)(end - start));start = System.currentTimeMillis();System.out.println("开始时间:" + System.currentTimeMillis());Test();System.out.println("结束时候用时:" + (long)(System.currentTimeMillis() - start));}public static void Test1(){int number = 0;int count = 0;String numberStr;for (int a = 1; a <= 5; a++){for (int b = 1; b <= 5; b++){int c=1;while(c<=5){for (int d = 1; d <= 5; d++){for (int e = 1; e <= 5; e++){// 打印出所有不重复的数字if (a != b && a != c && a != d && a != e && b != c && b != d && b != e && c != d && c != e && d != e){number = a * 10000 + b * 1000 + c * 100 + d * 10 + e;numberStr = String.valueOf(number);// 4不能再第3位, "3"与"5"不能相连if (numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1){count++;System.out.println(numberStr);}}}}c++;if(c==4) c++;}}}System.out.println("count=" + count);}public static void Test(){int number = 0;int count = 0;String numberStr;for (int a = 1; a <= 5; a++){for (int b = 1; b <= 5; b++){int c=1;if(a == b){continue;}while(c<=5){for (int d = 1; d <= 5; d++){if(a == d || b == d){continue;}for (int e = 1; e <= 5; e++){if(a == e || b == e || d == e){continue;}// 打印出所有不重复的数字if (a != c && b != c && c != d && c != e){number = a * 10000 + b * 1000 + c * 100 + d * 10 + e;numberStr = String.valueOf(number);// 4不能再第3位, "3"与"5"不能相连if (numberStr.indexOf("35") == -1 && numberStr.indexOf("53") == -1){count++;System.out.println(numberStr);}}}}c++;if(c==4) c++;}}}System.out.println("count=" + count);}}
方法四:
public static int n = 0;public static void main(String[] args) {List<String> list = new LinkedList<String>(Arrays.asList("1","2","3","4","5"));listAll(list, "");System.out.println(n); } public static void listAll(List<String> list, String strNum) { if (list.isEmpty()) { n++; System.out.println(strNum); } for (int i = 0; i < list.size(); i++) { int len = strNum.length(); List<String> tmp = new LinkedList<String>(list); String c = tmp.remove(i); if (len == 2 && c.charAt(0) == '4' // 第三个位置不能为4 || (len > 0 && c.charAt(0) == '3' && strNum.charAt(strNum.length() - 1) == '5') // 3和5不相连 || (len > 0 && c.charAt(0) == '5' && strNum.charAt(strNum.length() - 1) == '3')) { continue; } listAll(tmp, strNum + c); } }
0 0
- 面试杂谈,题目记录
- 面试题目记录
- 面试题目记录2
- 面试题目记录3
- 面试题目记录4
- 面试题目记录5
- 面试题目记录
- 面试题目,记录一下而已。
- 记录一些webservice面试题目
- 记录自己的java面试题目
- 值得记录的面试题目(算法工程师)
- 公司面试题目之取出数据库中重复的记录
- 公司面试题目之取出数据库中重复的记录
- Java笔试、面试中常见的题目记录
- 前端·面试 记录一些有意思的题目
- 公司面试题目之取出数据库中重复的记录
- 面试题目
- 面试题目
- HDOJ 4631 Sad Love Story
- IOS开发常用的开源类库6
- jfreechart 生成折线图,饼图,柱状图,堆栈柱状图
- hdu 1425 堆排序
- AOJ-AHU-OJ-662 越来越快
- 面试题目记录
- Huffman 编码压缩算法
- 创业必读:做一些“糟糕”的工作
- 一套UI控件
- Postion定位的疑惑
- 一致性 hash 算法
- BUPTOJ 0090 字符串转换
- C++ 公有继承、保护继承、私有继承的区别
- JAVA--异常