一著名软件公司的java笔试算法题!
一著名软件公司的java笔试算法题!
算法程序题:
该公司笔试题就1个,要求在10分钟内作完。
题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
问题点数:10、回复次数:150Top
1 楼lrk817()回复于 2006-10-09 11:02:45 得分 5
一个排列组合的问题!代码怎么个实现法?
期待答案!
顺便问一下:楼主知道一共有多少种输出吗?Top
2 楼microtea(小傅)回复于 2006-10-09 11:05:47 得分 0
想到一个笨方法,先一般排序,再用两个方法过滤"4"不能在第三位,"3"与"5"不能相连Top
3 楼akun_007(宝盖儿丁)回复于 2006-10-09 11:14:09 得分 5
public class A {
public static void main(String[] args) {
boolean third = true;
boolean connect1 = true;
boolean connect2 = true;
boolean connect3 = true;
boolean connect4 = true;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 5; j++) {
connect1 = true;
if((i == 3 && j == 5) || (i == 5 && j == 3)) {
connect1 = false;
}
for(int k = 1; k <= 5; k++ ) {
third = true;
connect2 = true;
if((k == 3 && j == 5) || (k == 5 && j == 3)) {
connect2 = false;
}
if(k == 4){
third = false;
}
for(int m = 1; m <= 5; m++ ) {
connect3 = true;
if((k == 3 && m == 5) || (k == 5 && m == 3)) {
connect3 = false;
}
for(int n = 1; n <= 5; n++ ) {
connect4 = true;
if((n == 3 && m == 5) || (n == 5 && m == 3)) {
connect4 = false;
}
if(third && connect1 && connect2 && connect3 && connect4) {
System.out.println(i + "" + j + "" + k + "" + m + "" + n + "");
}
}
}
}
}
}
}
}
用了差不多俩个10分钟Top
4 楼akun_007(宝盖儿丁)回复于 2006-10-09 11:22:12 得分 0
4分钟在纸上实现,6分钟敲代码,剩下的调试在8分钟内完成,还有1分钟回帖,差不多20分钟……Top
5 楼akun_007(宝盖儿丁)回复于 2006-10-09 11:23:02 得分 0
操!看错题了!!白忙了!!!Top
6 楼Johnsonwlf(勒苟拉斯)回复于 2006-10-09 11:45:12 得分 0
412345 怎么还有2个4?Top
7 楼yougucao379548695(郑州-小刘)回复于 2006-10-09 11:50:43 得分 0
5555555555555555555555Top
8 楼microtea(小傅)回复于 2006-10-09 12:07:37 得分 0
kao 用了我一个小时
package com.hkbea.math;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// System.out.println(Arith.div(10.2, 0.0));
int[] list = new int[] { 1, 2, 3, 4, 5 };
int[] templist;
int temp;
for (int i = 0; i < list.length; i++) {
templist = new int[] { list[0], list[1], list[2], list[3], list[4] };
temp = templist[i];
for (int j = 0; j < list.length; j++) {
// change position
if (j != i) {
templist[i] = templist[j];
templist[j] = temp;
}
if (templist[2] != 4) {
String valiList = "" + templist[0] + templist[1]
+ templist[2] + templist[3] + templist[4];
if (!(valiList.indexOf("35") > -1)
&& !(valiList.indexOf("53") > -1)) {
System.out.println(valiList);
}
}
if (j == 4)
System.out.println(" ");// split line
templist = new int[] { list[0], list[1], list[2], list[3],
list[4] };// clear templist
}
}
}
}
Top
9 楼sxg2005815()回复于 2006-10-09 12:10:18 得分 0
我认为使用数组应当更方便一点。Top
10 楼microtea(小傅)回复于 2006-10-09 12:13:32 得分 0
用数组是快,但不知逻辑会不会很烦?
我现在想到了"3+5=8"这个可能的条件入口Top
11 楼iambic()回复于 2006-10-09 12:17:03 得分 0
1-2-2-3-4-5 六个数字!412345怎么是两个4?Top
12 楼akun_007(宝盖儿丁)回复于 2006-10-09 12:36:58 得分 0
楼主把我的帖子删了吧,留着怪丢人的。Top
13 楼jx321(太极)回复于 2006-10-09 12:38:06 得分 0
up 学习~~Top
14 楼Siela(伱儅硪寔倁己硪看嘚噵 硪儅伱寔一笙偂途)回复于 2006-10-09 12:45:00 得分 0
小弟觉得一共有(排列组合)
所有放置放法一共有P66种
4在第三个位置的放法有P55种
3和5在一起的放置方法有5×P44种
所以一共有P66 - P55 - 5*P44种
不知道对不对,以前的排列组合都忘了Top
15 楼harston(顽石)(风停了,雨停了,一路泥泞~)回复于 2006-10-09 12:52:24 得分 0
楼上的理解错了,不是算数目,而是让你把所有排列输出来Top
16 楼seakingwy(JAVAing~重新开始)回复于 2006-10-09 12:53:57 得分 0
跟着学习...Top
17 楼liushuguang()回复于 2006-10-09 13:06:18 得分 0
大家好,我是一个刚刚学了一点Java的小小菜鸟,希望各位大虾能多多伸出援助之小手拉小弟一把,不胜感激!!1Top
18 楼leiyongekin()回复于 2006-10-09 13:24:57 得分 0
想请问第四楼的 你的方法 可能出现 这样的结果
11234 。。。。
这就不行了。。。Top
19 楼leiyongekin()回复于 2006-10-09 13:26:06 得分 0
想了一下 还是只能用一个数组来实现 。。。Top
20 楼yleiou(单刀匹马)回复于 2006-10-09 13:40:25 得分 0
mark 有些难度Top
21 楼dvdface(汉学堂)回复于 2006-10-09 13:45:32 得分 0
按理说应该用递归或者迭代法做。Top
22 楼eagleking012((菜鸟也疯狂))回复于 2006-10-09 13:49:16 得分 0
public class Pailie {
public static void main(String[] args) {
int[] temp = new int[6];
int[] num = {
1, 2, 2, 3, 4, 5};
for (int s1 = 0; s1 < 6; s1++) {
temp[s1] = num[0];
for (int s2 = 0; s2 < 6; s2++) {
if (s2 == s1) {
continue;
}
temp[s2] = num[1];
for (int s3 = 0; s3 < 6; s3++) {
if (s3 == s1 || s3 == s2) {
continue;
}
temp[s3] = num[2];
for (int s4 = 0; s4 < 6; s4++) {
if(s4==s1 || s4 ==s2 || s4 ==s3){
continue;
}
temp[s4] = num[3];
for(int s5=0;s5<6;s5++){
if(s5 ==s1 || s5 ==s2 || s5 ==s3 || s5 ==s4){
continue;
}
temp[s5] = num[4];
for(int s6=0;s6<6;s6++){
if(s6 ==s1 || s6==s2 || s6==s3 || s6==s4 || s6==s5){
continue;
}
temp[s6] = num[5];
String pstr = String.valueOf(temp[0])+String.valueOf(temp[1])+String.valueOf(temp[2])+String.valueOf(temp[3])+String.valueOf(temp[4])+String.valueOf(temp[5]);
if(!(pstr.indexOf("4")==3 || pstr.indexOf("35")>0 || pstr.indexOf("53")>0))
System.out.println(pst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4
5 1 3 2 4 2
5 1 3 4 2 2
5 2 1 2 3 4
5 2 1 2 4 3
5 2 1 3 2 4
5 2 1 3 4 2
5 2 1 4 2 3
5 2 1 4 3 2
5 2 2 1 3 4
5 2 2 1 4 3
5 2 2 3 1 4
5 2 2 3 4 1
5 2 2 4 1 3
5 2 2 4 3 1
5 2 3 1 2 4
5 2 3 1 4 2
5 2 3 2 1 4
5 2 3 2 4 1
5 2 3 4 1 2
5 2 3 4 2 1
5 4 1 2 2 3
5 4 1 2 3 2
5 4 1 3 2 2
5 4 2 1 2 3
5 4 2 1 3 2
5 4 2 2 1 3
5 4 2 2 3 1
5 4 2 3 1 2
5 4 2 3 2 1
5 4 3 1 2 2
5 4 3 2 1 2
5 4 3 2 2 1Top
24 楼jadefoxma()回复于 2006-10-09 14:04:24 得分 0
我个人的想法是:
①将六个数分别放到数组中,在设一个输出空字符串。
②然后写一个六级嵌套for循环,把每一级的数组值用字符串“+”的方法连接。
②最里层for循环为输出字符串,在输出前判断:"4"不能在第三位,"3"与"5"不能相连。要是符合就输出,要是不符合就不输出。
思路是这样,至于代码自己随便一些就可以了吧。Top
25 楼leiyongekin()回复于 2006-10-09 14:05:26 得分 0
楼上的正解。。。不过 每次都要比较一次 相等不相等 是不是有点麻烦?Top
26 楼eagleking012((菜鸟也疯狂))回复于 2006-10-09 14:06:16 得分 0
public class Pailie {
public static void main(String[] args) {
int[] temp = new int[6];
int[] num = {
1, 2, 2, 3, 4, 5};
for (int s1 = 0; s1 < 6; s1++) {
temp[s1] = num[0];
for (int s2 = 0; s2 < 6; s2++) {
if (s2 == s1) {
continue;
}
temp[s2] = num[1];
for (int s3 = 0; s3 < 6; s3++) {
if (s3 == s1 || s3 == s2) {
continue;
}
temp[s3] = num[2];
for (int s4 = 0; s4 < 6; s4++) {
if(s4==s1 || s4 ==s2 || s4 ==s3){
continue;
}
temp[s4] = num[3];
for(int s5=0;s5<6;s5++){
if(s5 ==s1 || s5 ==s2 || s5 ==s3 || s5 ==s4){
continue;
}
temp[s5] = num[4];
for(int s6=0;s6<6;s6++){
if(s6 ==s1 || s6==s2 || s6==s3 || s6==s4 || s6==s5){
continue;
}
temp[s6] = num[5];
String pstr = String.valueOf(temp[0])+String.valueOf(temp[1])+String.valueOf(temp[2])+String.valueOf(temp[3])+String.valueOf(temp[4])+String.valueOf(temp[5]);
if(!(pstr.indexOf("4")==3 || pstr.indexOf("35")>0 || pstr.indexOf("53")>0))
System.out.println(pstr);
else
System.out.println("不合法"+pstr);
}
}
}
}
}
}
}
}Top
27 楼eagleking012((菜鸟也疯狂))回复于 2006-10-09 14:06:57 得分 0
结果为122345
不合法122354
不合法122435
不合法122534
不合法122453
122543
123245
123254
不合法124235
125234
不合法124253
125243
不合法123425
不合法123524
124325
不合法125324
124523
不合法125423
不合法123452
不合法123542
不合法124352
不合法125342
不合法124532
不合法125432
122345
不合法122354
不合法122435
不合法122534
不合法122453
122543
132245
132254
不合法142235
152234
不合法142253
152243
不合法132425
132524
142325
152324
142523
不合法152423
不合法132452
132542
不合法142352
152342
不合法142532
不合法152432
123245
123254
不合法124235
125234
不合法124253
125243
132245
132254
不合法142235
152234
不合法142253
152243
134225
不合法135224
143225
不合法153224
145223
154223
134252
不合法135242
143252
不合法153242
145232
154232
不合法123425
不合法123524
124325
不合法125324
124523
不合法125423
不合法132425
132524
142325
152324
142523
不合法152423
134225
不合法135224
143225
不合法153224
145223
154223
134522
不合法135422
不合法143522
不合法153422
不合法145322
154322
不合法123452
不合法123542
不合法124352
不合法125342
不合法124532
不合法125432
不合法132452
132542
不合法142352
152342
不合法142532
不合法152432
134252
不合法135242
143252
不合法153242
145232
154232
134522
不合法135422
不合法143522
不合法153422
不合法145322
154322
212345
不合法212354
不合法212435
不合法212534
不合法212453
212543
213245
213254
不合法214235
215234
不合法214253
215243
不合法213425
不合法213524
214325
不合法215324
214523
不合法215423
不合法213452
不合法213542
不合法214352
不合法215342
不合法214532
不合法215432
212345
不合法212354
不合法212435
不合法212534
不合法212453
212543
312245
312254
不合法412235
512234
不合法412253
512243
不合法312425
312524
412325
512324
412523
不合法512423
不合法312452
312542
不合法412352
512342
不合法412532
不合法512432
213245
213254
不合法214235
215234
不合法214253
215243
312245
312254
不合法412235
512234
不合法412253
512243
314225
315224
413225
513224
415223
514223
314252
315242
413252
513242
415232
514232
不合法213425
不合法213524
214325
不合法215324
214523
不合法215423
不合法312425
312524
412325
512324
412523
不合法512423
314225
315224
413225
513224
415223
514223
314522
不合法315422
不合法413522
不合法513422
不合法415322
514322
不合法213452
不合法213542
不合法214352
不合法215342
不合法214532
不合法215432
不合法312452
312542
不合法412352
512342
不合法412532
不合法512432
314252
315242
413252
513242
415232
514232
314522
不合法315422
不合法413522
不合法513422
不合法415322
514322
221345
不合法221354
不合法221435
不合法221534
不合法221453
221543
231245
231254
不合法241235
251234
不合法241253
251243
不合法231425
231524
241325
251324
241523
不合法251423
不合法231452
231542
不合法241352
251342
不合法241532
不合法251432
221345
不合法221354
不合法221435
不合法221534
不合法221453
221543
321245
321254
不合法421235
521234
不合法421253
521243
不合法321425
321524
421325
521324
421523
不合法521423
不合法321452
321542
不合法421352
521342
不合法421532
不合法521432
231245
231254
不合法241235
251234
不合法241253
251243
321245
321254
不合法421235
521234
不合法421253
521243
341225
351224
431225
531224
451223
541223
341252
351242
431252
531242
451232
541232
不合法231425
231524
241325
251324
241523
不合法251423
不合法321425
321524
421325
521324
421523
不合法521423
341225
351224
431225
531224
451223
541223
341522
不合法351422
431522
不合法531422
451322
541322
不合法231452
231542
不合法241352
251342
不合法241532
不合法251432
不合法321452
321542
不合法421352
521342
不合法421532
不合法521432
341252
351242
431252
531242
451232
541232
341522
不合法351422
431522
不合法531422
451322
541322
223145
223154
不合法224135
225134
不合法224153
225143
232145
232154
不合法242135
252134
不合法242153
252143
234125
不合法235124
243125
不合法253124
245123
254123
234152
不合法235142
243152
不合法253142
245132
254132
223145
223154
不合法224135
225134
不合法224153
225143
322145
322154
不合法422135
522134
不合法422153
522143
324125
325124
423125
523124
425123
524123
324152
325142
423152
523142
425132
524132
232145
232154
不合法242135
252134
不合法242153
252143
322145
322154
不合法422135
522134
不合法422153
522143
342125
352124
432125
532124
452123
542123
342152
352142
432152
532142
452132
542132
234125
不合法235124
243125
不合法253124
245123
254123
324125
325124
423125
523124
425123
524123
342125
352124
432125
532124
452123
542123
345122
354122
不合法435122
534122
不合法453122
543122
234152
不合法235142
243152
不合法253142
245132
254132
324152
325142
423152
523142
425132
524132
342152
352142
432152
532142
452132
542132
345122
354122
不合法435122
534122
不合法453122
543122
不合法223415
不合法223514
224315
不合法225314
224513
不合法225413
不合法232415
232514
242315
252314
242513
不合法252413
234215
不合法235214
243215
不合法253214
245213
254213
234512
不合法235412
不合法243512
不合法253412
不合法245312
254312
不合法223415
不合法223514
224315
不合法225314
224513
不合法225413
不合法322415
322514
422315
522314
422513
不合法522413
324215
325214
423215
523214
425213
524213
324512
不合法325412
不合法423512
不合法523412
不合法425312
524312
不合法232415
232514
242315
252314
242513
不合法252413
不合法322415
322514
422315
522314
422513
不合法522413
342215
352214
432215
532214
452213
542213
342512
不合法352412
432512
不合法532412
452312
542312
234215
不合法235214
243215
不合法253214
245213
254213
324215
325214
423215
523214
425213
524213
342215
352214
432215
532214
452213
542213
345212
354212
不合法435212
534212
不合法453212
543212
234512
不合法235412
不合法243512
不合法253412
不合法245312
254312
324512
不合法325412
不合法423512
不合法523412
不合法425312
524312
342512
不合法352412
432512
不合法532412
452312
542312
345212
354212
不合法435212
534212
不合法453212
543212
不合法223451
不合法223541
不合法224351
不合法225341
不合法224531
不合法225431
不合法232451
232541
不合法242351
252341
不合法242531
不合法252431
234251
不合法235241
243251
不合法253241
245231
254231
234521
不合法235421
不合法243521
不合法253421
不合法245321
254321
不合法223451
不合法223541
不合法224351
不合法225341
不合法224531
不合法225431
不合法322451
322541
不合法422351
522341
不合法422531
不合法522431
324251
325241
423251
523241
425231
524231
324521
不合法325421
不合法423521
不合法523421
不合法425321
524321
不合法232451
232541
不合法242351
252341
不合法242531
不合法252431
不合法322451
322541
不合法422351
522341
不合法422531
不合法522431
342251
352241
432251
532241
452231
542231
342521
不合法352421
432521
不合法532421
452321
542321
234251
不合法235241
243251
不合法253241
245231
254231
324251
325241
423251
523241
425231
524231
342251
352241
432251
532241
452231
542231
345221
354221
不合法435221
534221
不合法453221
543221
234521
不合法235421
不合法243521
不合法253421
不合法245321
254321
324521
不合法325421
不合法423521
不合法523421
不合法425321
524321
342521
不合法352421
432521
不合法532421
452321
542321
345221
354221
不合法435221
534221
不合法453221
543221Top
28 楼eagleking012((菜鸟也疯狂))回复于 2006-10-09 14:20:02 得分 0
上面有误对于53或35打头的应该是indexOf("")>=0
正解为:
public class Pailie {
public static void main(String[] args) {
int[] temp = new int[6];
int[] num = {
1, 2, 2, 3, 4, 5};
for (int s1 = 0; s1 < 6; s1++) {
temp[s1] = num[0];
for (int s2 = 0; s2 < 6; s2++) {
if (s2 == s1) {
continue;
}
temp[s2] = num[1];
for (int s3 = 0; s3 < 6; s3++) {
if (s3 == s1 || s3 == s2) {
continue;
}
temp[s3] = num[2];
for (int s4 = 0; s4 < 6; s4++) {
if(s4==s1 || s4 ==s2 || s4 ==s3){
continue;
}
temp[s4] = num[3];
for(int s5=0;s5<6;s5++){
if(s5 ==s1 || s5 ==s2 || s5 ==s3 || s5 ==s4){
continue;
}
temp[s5] = num[4];
for(int s6=0;s6<6;s6++){
if(s6 ==s1 || s6==s2 || s6==s3 || s6==s4 || s6==s5){
continue;
}
temp[s6] = num[5];
String pstr = String.valueOf(temp[0])+String.valueOf(temp[1])+String.valueOf(temp[2])+String.valueOf(temp[3])+String.valueOf(temp[4])+String.valueOf(temp[5]);
if(!(pstr.indexOf("4")==3 || pstr.indexOf("35")>=0 || pstr.indexOf("53")>=0))
System.out.println(pstr);
//else
//System.out.println("不合法"+pstr);
}
}
}
}
}
}
}
}
共396种
6*5*4*3*2*1 -5*4*3*2*1(4在第三位)-2*(5*4*3*2*1)(53或35)+2*(3*3*2*1)(4在三位53或35相连) =396 种Top
29 楼leiyongekin()回复于 2006-10-09 14:28:15 得分 0
菜鸟也疯狂。。。。。
高手。。。。。你的的确是正解。Top
30 楼Could(翻墙鹦鹉)回复于 2006-10-09 14:28:33 得分 0
如何去掉重复的啊?
因为有2个2,所以有重复的,
用什么方法把重复的去掉好?
Top
31 楼ghbiou(ghbiou)回复于 2006-10-09 14:49:38 得分 0
29358种
#include <iostream>
using namespace std;
//题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,
// 如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。
void main()
{
int a,b,c,d,e,f;
//int a[6]={1,2,3,4,5,6};
long count=0;
for (a=1;a<= 6;a++)
for (b=1;b<= 6;b++)
for (c=1;c<= 6;c++)
for (d=1;d<= 6;d++)
for (e=1;e<= 6;e++)
for (f=1;f<= 6;f++)
{
if (c!=4)
{
if (((a!=3 && b!=5) || (a!=5 && b!=3)) && \
((b!=3 && c!=5) || (b!=5 && c!=3)) && \
((c!=3 && d!=5) || (c!=5 && d!=3)) && \
((d!=3 && e!=5) || (d!=5 && e!=3)) && \
((e!=3 && f!=5) || (e!=5 && f!=3)) )
{
printf("%d%d%d%d%d%d\n",a,b,c,d,e,f);
count++;
}
}
}
printf("the count is: %d",count);
}Top
32 楼ghbiou(ghbiou)回复于 2006-10-09 14:51:19 得分 0
共396种
你好好想想,不可能这么点吧!!Top
33 楼liangkandy2002(树大招风)回复于 2006-10-09 14:53:23 得分 0
想了问题,正想睡觉。Top
34 楼tempture()回复于 2006-10-09 15:07:33 得分 0
我算出来也就396种Top
35 楼eagleking012((菜鸟也疯狂))回复于 2006-10-09 15:09:32 得分 0
是哪个公司的题目啊 ,就这么一道?Top
36 楼fdlm_dark()回复于 2006-10-09 15:24:39 得分 0
有两个2阿。。。不好搞继续写。。。Top
37 楼inorro()回复于 2006-10-09 15:28:25 得分 0
chrisli1983(康仔) 是正解!!Top
38 楼dongfei(风月无影)回复于 2006-10-09 15:30:01 得分 0
a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:2 f:4
a:5 b:2 c:1 d:3 e:4 f:2
a:5 b:2 c:1 d:4 e:2 f:3
a:5 b:2 c:1 d:4 e:3 f:2
a:5 b:2 c:2 d:1 e:3 f:4
a:5 b:2 c:2 d:1 e:4 f:3
a:5 b:2 c:2 d:3 e:1 f:4
a:5 b:2 c:2 d:3 e:4 f:1
a:5 b:2 c:2 d:4 e:1 f:3
a:5 b:2 c:2 d:4 e:3 f:1
a:5 b:2 c:3 d:1 e:2 f:4
a:5 b:2 c:3 d:1 e:4 f:2
a:5 b:2 c:3 d:2 e:1 f:4
a:5 b:2 c:3 d:2 e:4 f:1
a:5 b:2 c:3 d:4 e:1 f:2
a:5 b:2 c:3 d:4 e:2 f:1
a:5 b:4 c:1 d:2 e:2 f:3
a:5 b:4 c:1 d:2 e:3 f:2
a:5 b:4 c:1 d:3 e:2 f:2
a:5 b:4 c:2 d:1 e:2 f:3
a:5 b:4 c:2 d:1 e:3 f:2
a:5 b:4 c:2 d:2 e:1 f:3
a:5 b:4 c:2 d:2 e:3 f:1
a:5 b:4 c:2 d:3 e:1 f:2
a:5 b:4 c:2 d:3 e:2 f:1
a:5 b:4 c:3 d:1 e:2 f:2
a:5 b:4 c:3 d:2 e:1 f:2
a:5 b:4 c:3 d:2 e:2 f:1
count:198Top
39 楼fdlm_dark()回复于 2006-10-09 15:34:30 得分 0
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static private char setNumber(int num)
{
char a=' ';
switch(num)
{
case 0:
a = '1';
break;
case 1:
a = '2';
break;
case 2:
a = '2';
break;
case 3:
a = '3';
break;
case 4:
a = '4';
break;
case 5:
a = '5';
break;
}
return a;
}
static private bool valString(char[] temp,string temp1)
{
string a;
a = temp[0].ToString() + temp[1].ToString() + temp[2].ToString() + temp[3].ToString() + temp[4].ToString() + temp[5].ToString();
bool ret = true;
if (a.IndexOf("35") > -1 || a.IndexOf("53") > -1 || a.Substring(2, 1) == "4")
ret = false;
int flag1, flag2;
flag1 = temp1.IndexOf("2");
flag2 = temp1.IndexOf("1");
if (flag2 > flag1)
ret = false;
return ret;
}
static void Main(string[] args)
{
int a,b,c,d,e,f,i=0;
string abcdef;
char[] temp= new char[6];
for (a=0;a<6;a++)
{
temp[0]= setNumber(a);
for (b=0;b<6;b++)
{
if (b == a) continue;
temp[1] = setNumber(b);
for (c=0;c<6;c++)
{
if (c == b || c == a) continue;
temp[2] = setNumber(c);
for (d=0;d<6;d++)
{
if (d == c || d == b || d == a) continue;
temp[3] = setNumber(d);
for (e=0;e<6;e++)
{
if (e == d || e == c || e == b || e == a) continue;
temp[4] = setNumber(e);
for (f=0;f<6;f++)
{
if (f == a || f == b || f == c || f == d || f == e) continue;
temp[5] = setNumber(f);
abcdef = a.ToString() + b.ToString() + c.ToString() + d.ToString() + e.ToString() + f.ToString();
if (valString(temp,abcdef))
{
i=i+1;
System.Console.WriteLine(temp);
}
}
}
}
}
}
}
System.Console.WriteLine(i);
}
}
}
198种没错。Top
40 楼inorro()回复于 2006-10-09 15:36:58 得分 0
dongfei(风月无影) 也对,贴代码Top
41 楼leiyongekin()回复于 2006-10-09 15:46:30 得分 0
我想请问,,怎么可能是198。
用数学方法先算出来 都是396。。。
198是怎么算出来的 用数学的方法好象 菜鸟也疯狂的方法一点错误也没有啊!
请高手来解答!Top
42 楼fdlm_dark()回复于 2006-10-09 15:49:23 得分 0
122345
122543
123245
123254
123425
123452
125234
125243
125423
125432
132245
132254
132425
132452
132524
132542
142325
142523
143225
143252
145223
145232
152234
152243
152324
152342
152423
152432
212345
212543
213245
213254
213425
213452
215234
215243
215423
215432
221345
221543
223145
223154
223415
223451
225134
225143
225413
225431
231245
231254
231425
231452
231524
231542
232145
232154
232415
232451
232514
232541
241325
241523
242315
242513
243125
243152
243215
243251
245123
245132
245213
245231
251234
251243
251324
251342
251423
251432
252134
252143
252314
252341
252413
252431
312245
312254
312425
312452
312524
312542
315224
315242
315422
321245
321254
321425
321452
321524
321542
322145
322154
322415
322451
322514
322541
325124
325142
325214
325241
325412
325421
341225
341252
341522
342125
342152
342215
342251
342512
342521
345122
345212
345221
412325
412523
413225
413252
415223
415232
421325
421523
422315
422513
423125
423152
423215
423251
425123
425132
425213
425231
431225
431252
431522
432125
432152
432215
432251
432512
432521
451223
451232
451322
452123
452132
452213
452231
452312
452321
512234
512243
512324
512342
512423
512432
513224
513242
513422
521234
521243
521324
521342
521423
521432
522134
522143
522314
522341
522413
522431
523124
523142
523214
523241
523412
523421
541223
541232
541322
542123
542132
542213
542231
542312
542321
543122
543212
543221
total:198Top
43 楼fdlm_dark()回复于 2006-10-09 15:50:27 得分 0
因为有两个2如果区分一下
5 4 3 2 "2" 1和 5 4 3 "2" 2 1输出的结果一样所以需要去掉一半的结果。Top
44 楼leiyongekin()回复于 2006-10-09 15:53:10 得分 0
呵呵 谢谢。。。。那在数学的基础上应该还减去,两个2在一起的一半的个数。。
Top
45 楼thistl()回复于 2006-10-09 15:53:15 得分 0
6*5*4*3*2*1 -5*4*3*2*1(4在第三位)-2*(5*4*3*2*1)(53或35)+2*(3*3*2*1)(4在三位53或35相连) =396 种
这种方法的错误在于 没有考虑两个2重复的情况 所以应该396/2=198Top
46 楼fdlm_dark()回复于 2006-10-09 16:01:08 得分 0
用了哥们一小时。。看样子要失业呵呵。Top
47 楼eagleking012((菜鸟也疯狂))回复于 2006-10-09 16:02:36 得分 0
又没说两个2是一样的来着
呵呵 那把排出来的放到set里 或者去掉重复的就是198种了Top
48 楼leiyongekin()回复于 2006-10-09 16:11:55 得分 0
请问 DARK 这段代码是什么意思?
int flag1, flag2;
flag1 = temp1.IndexOf("2");
flag2 = temp1.IndexOf("1");
if (flag2 > flag1)
ret = false;
Top
49 楼fdlm_dark()回复于 2006-10-09 16:18:11 得分 0
这个就是排出了2的重复
因为我的setNumber函数里2 和 1输出都是'2'
所以可能出现两种情况2 1或1 2
我这里只承认1 2这种组合合法。2 1的全部false掉。Top
50 楼fdlm_dark()回复于 2006-10-09 16:19:26 得分 0
哦对了。没有java环境拿c#写的。Top
51 楼w5211(笑幺)回复于 2006-10-09 16:24:52 得分 0
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int k = 0;
for (int i = 122345; i <=543221 ; i++)
{
string a = i.ToString();
if (vv(a, "1") && v2(a) && vv(a, "3") && v4(a) && vv

