122345java的简单介绍
今天给各位分享122345java的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、JAVA用1、2、2、3、4、5排列组合,最多能排列多少组合并打印出来。要求:4不能放在第三位,4和5不能相连
- 2、java排列组合算法?
- 3、1,2,3,4,5,用java写一个程序,排出不同的排序,如:12345,34512...,要求3与5不能相连。
- 4、java如何实现按行读取键盘输入?
- 5、用122345六个数用Java写一个main函数打印出所有不同的排序
JAVA用1、2、2、3、4、5排列组合,最多能排列多少组合并打印出来。要求:4不能放在第三位,4和5不能相连
算法程序题:
该公司笔试题就1个,要求在10分钟内作完。
题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
static int[] bits = new int[] { 1, 2, 3, 4, 5 };
/**
* @param args
*/
public static void main(String[] args) {
sort("", bits);
}
private static void sort(String prefix, int[] a) {
if (a.length == 1) {
System.out.println(prefix + a[0]);
}
for (int i = 0; i a.length; i++) {
sort(prefix + a[i], copy(a, i));
}
}
private static int[] copy(int[] a,int index){
int[] b = new int[a.length-1];
System.arraycopy(a, 0, b, 0, index);
System.arraycopy(a, index+1, b, index, a.length-index-1);
return b;
}
**********************************************************************
基本思路:
1 把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。
2 显然这个结果集还未达到题目的要求。从以下几个方面考虑:
1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。
采用二维数组定义图结构,最后的代码是:
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet set = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i n; i++) {
for (int j = 0; j n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j n; j++) {
if (a[startIndex][j] == 1 visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
java排列组合算法?
//这个程序是以前用高分求来的,现在稍作修改,呵呵
public class Zuhe {
public static void main(String[] args) {
String s = "122345";//这里是要用到的所有数组成的一个字符串,其它字符同样适用
char[] c = s.toCharArray();
new Zuhe().zuhe(c,c.length,0);
System.out.println("可能的组合数:"+kk);
}
static int kk=0;
private void zuhe(char[] array, int n, int k) {
if (n == k) {
if(array[2]!='4'){//第三个位置不能出现4
String str = new String(array);
if(str.indexOf("53")0str.indexOf("35")0){//3,5不能连续出现
System.out.println(str);
++kk;
}
}
} else {
for (int i = k; i n; i++) {
swap(array, k, i);
zuhe(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
========结果=========
122345
122543
123245
123254
123425
123452
125432
125423
125243
125234
122345
122543
123245
123254
123425
123452
125432
125423
125243
125234
132245
132254
132425
132452
132542
132524
132245
132254
132425
132452
132542
132524
142325
142523
143225
143252
143225
143252
142325
142523
145232
145223
145223
145232
152342
152324
152432
152423
152243
152234
152342
152324
152432
152423
152243
152234
212345
212543
213245
213254
213425
213452
215432
215423
215243
215234
221345
221543
223145
223154
223415
223451
225431
225413
225143
225134
232145
232154
232415
232451
232541
232514
231245
231254
231425
231452
231542
231524
242315
242513
243215
243251
243125
243152
241325
241523
245132
245123
245213
245231
252341
252314
252431
252413
252143
252134
251342
251324
251432
251423
251243
251234
221345
221543
223145
223154
223415
223451
225431
225413
225143
225134
212345
212543
213245
213254
213425
213452
215432
215423
215243
215234
231245
231254
231425
231452
231542
231524
232145
232154
232415
232451
232541
232514
241325
241523
243125
243152
243215
243251
242315
242513
245231
245213
245123
245132
251342
251324
251432
251423
251243
251234
252341
252314
252431
252413
252143
252134
322145
322154
322415
322451
322541
322514
321245
321254
321425
321452
321542
321524
325142
325124
325412
325421
325241
325214
322145
322154
322415
322451
322541
322514
321245
321254
321425
321452
321542
321524
325142
325124
325412
325421
325241
325214
312245
312254
312425
312452
312542
312524
312245
312254
312425
312452
312542
312524
315242
315224
315422
315422
315242
315224
342125
342152
342215
342251
342521
342512
341225
341252
341225
341252
341522
341522
342125
342152
342215
342251
342521
342512
345122
345122
345212
345221
345221
345212
422315
422513
423215
423251
423125
423152
421325
421523
425132
425123
425213
425231
422315
422513
423215
423251
423125
423152
421325
421523
425132
425123
425213
425231
432215
432251
432125
432152
432512
432521
432215
432251
432125
432152
432512
432521
431225
431252
431225
431252
431522
431522
412325
412523
413225
413252
413225
413252
412325
412523
415232
415223
415223
415232
452312
452321
452132
452123
452213
452231
451322
451322
451232
451223
451223
451232
452312
452321
452132
452123
452213
452231
522341
522314
522431
522413
522143
522134
523241
523214
523421
523412
523142
523124
521342
521324
521432
521423
521243
521234
522341
522314
522431
522413
522143
522134
523241
523214
523421
523412
523142
523124
521342
521324
521432
521423
521243
521234
542321
542312
542231
542213
542123
542132
543221
543212
543221
543212
543122
543122
542321
542312
542231
542213
542123
542132
541322
541322
541232
541223
541223
541232
512342
512324
512432
512423
512243
512234
513242
513224
513422
513422
513242
513224
512342
512324
512432
512423
512243
512234
可能的组合数:396
1,2,3,4,5,用java写一个程序,排出不同的排序,如:12345,34512...,要求3与5不能相连。
public class GroupTest {
public static ListString list = new ArrayListString();
/**
* 构造字符串的所有排序组合
*
* @param str 将要组合成的字符
* @param nstr 源字符串集
*/
public static void group(String str, String nstr) {
if (str.length() != nstr.length()) {
String rest = getRest(str, nstr);
for (int i = 0; i rest.length(); i++) {
String temp = str + rest.substring(i, i + 1);
if (temp.indexOf("4") != 2 temp.indexOf("35") == -1 temp.indexOf("53") == -1) {// 过滤显示条件,如果去掉此处的判断,就是列出所有字符集的排列组合
System.out.println(temp);
if (!list.contains(temp)) {
list.add(temp);
}
group(temp, nstr);
}
}
}
}
/**
* 从源字符串集中去除将要组合成的字符
*
* @param str 将要组合成的字符
* @param nstr 源字符串集
* @return 剩余字符串集
*/
public static String getRest(String str, String nstr) {
String rest = "";
if (nstr.length() str.length()) {
rest = nstr;
for (int i = 0; i str.length(); i++) {
rest = rest.replaceFirst(str.substring(i, i + 1), "");// 注意此处的replaceFirst,而不是replaceAll
}
}
return rest;
}
public static void main(String[] args) {
group("", "122345");
System.out.println(list.toString());
}
}
java如何实现按行读取键盘输入?
亲,,你应该先搞明白while和do while
再问问题,,很明显,你这个的原因是while实现判断循环条件在判断是否循环,,
你换成do while试试
用122345六个数用Java写一个main函数打印出所有不同的排序
public static void main(String[] args) {
int[] array = { 1, 2, 2, 3, 4, 5 };
int index = 0; // 新数组下标
int temp = 0; // 每一个数
int length = 0; // 新数组长度
int[] newArray;// 新数组
// 求出新数组长度
for (int i = 0; i array.length; i++) {
for (int j = 0; j array.length; j++) {
for (int k = 0; k array.length; k++) {
for (int m = 0; m array.length; m++) {
for (int n = 0; n array.length; n++) {
for (int x = 0; x array.length; x++) {
if (i != j j != k i != k i != m
j != m k != m i != n j != n
k != n m != n i != x j != x
k != x m != x n != x) {
length++;
}
}
}
}
}
}
}
newArray = new int[length];// 确定新数组长度
// 为新数组赋值
for (int i = 0; i array.length; i++) {
for (int j = 0; j array.length; j++) {
for (int k = 0; k array.length; k++) {
for (int m = 0; m array.length; m++) {
for (int n = 0; n array.length; n++) {
for (int x = 0; x array.length; x++) {
if (i != j j != k i != k i != m
j != m k != m i != n j != n
k != n m != n i != x j != x
k != x m != x n != x) {
temp = array[i] * 100000 + array[j] * 10000
+ array[k] * 1000 + array[m] * 100
+ array[n] * 10 + array[x];
newArray[index] = temp;
index++;
}
}
}
}
}
}
}
//遍历新数组,遇到相同的值,只留其中一个,其他全部为0
for (int i = 0; i newArray.length; i++) {
for (int j = i; j newArray.length - 1; j++) {
if (newArray[i] == newArray[j + 1]) {
newArray[j + 1] = 0;
}
}
}
//输出所有不为0的数字,就是最后结果
for (int i = 0; i newArray.length; i++) {
if (newArray[i] != 0) {
System.out.print(newArray[i] + " ");
}
}
}
//所有代码,我已经格式化过了
关于122345java和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。