这个题不太明白,望高手指教
六个数字,1,2,3,3,4,5,打印出所有六个数字的六位数组合,如:123345,313452等,要求:4不在第三位,3和5不挨着
六个数字,1,2,3,3,4,5,打印出所有六个数字的六位数组合,如:123345,313452等,要求:4不在第三位,3和5不挨着
解决方案 »
- Java中的所有类型转换
- 最后两行为什么输出3.0 而不是3.333333和3.3333333333333333呢?
- 为什么用Junit测试会这么慢?
- 我想查看javax.servlet.*和java.servlet.http.*的函数的说明,有没有他api文档
- 一个关于JNI调用DLL导致JVM崩溃的问题!!!
- 关于JDK版本冲突的问题,如何把oracle代的JDK1.3替换成1.4X?
- mysql插不进去数据的问题
- 关于java小程序和应用程序的问题
- ArrayList和Vector有何区别?
- 看看下面的小程序,错在哪里了?
- 谁来帮我讲解一下这道题~~~谢谢
- 关于文件及文件夹操作的的一个问题。
用6个数组,也就是对应六位
然后一个一个嵌套循环就可以了要求1:第三位的数组不要放4,
要求2:如果外面的循环的值是3或5,当前的循环是5或3的时候,就continue手边没有编译器,不知道能不能实现good luck
public class Test {
public static void main(String[] args) { for (int i = 111111; i <= 555555; i++) {
String s = String.valueOf(i);
if (s.charAt(2) == '4') {
continue;
}
int index_3 = s.indexOf("3");
if (index_3 == 5 && s.charAt(index_3 - 1) == '5') {
continue;
} else if (index_3 == 0 && s.charAt(index_3 + 1) == '5') {
continue;
} else if (index_3 > 0
&& index_3 < 5
&& (s.charAt(index_3 - 1) == '5' || s.charAt(index_3 + 1) == '5')) {
continue;
} else {
System.out.println(i);
}
} }
}如果就是1,2,3,4,5就这5个数还可以
要是有别的数我想还要比较一下
import java.util.Date;
public class TestPL{
static boolean equal(int i,int j,int k,int x,int y,int z){
//判断六个数是否有相同的数,有则返回TRUE,否则返回FALSE
if(i == j||i == k||i==x||i==y||i==z||j==k||j==x||j==y||j==z||k==x||k==y||k==z||x==y||x==z||y==z)
//六个数中只要有一对数相等则返回TRUE
return true;
else
return false;
}
public static void main(String[] args){
String str1 = "";
String str = "";
int[] a = {1,2,2,3,4,5};
int count = 0; //排列总数
for(int i = 0;i < a.length;i++){
for(int j = 0;j < a.length;j++){
for(int k = 0;k < a.length;k++){
//if(a[i]!=a[j]&&a[i]!=a[k]&&a[j]!=a[k])
for(int x = 0;x < a.length;x++){
for(int y = 0;y < a.length;y++){
for(int z = 0;z < a.length;z++){
if(!equal(i,j,k,x,y,z)&&a[x]!=4){//调用上面的函数,注意用到了非运算符"!"
//以下判断3,5不相邻的情况(穷举法,因为3,5在一起只有10种情况)
if(!((i == 3&&j == 5)||(j == 3&&k == 5)||(k == 3&&x == 5)||(x == 3&&y == 5)||(y == 3&&z == 5)||(i==5&&j==3)||(j==5&&k==3)||(k==5&&x==3)||(x==5&&y==3)||(y==5&&z==3))){
//count ++;
str1 = ""+a[i] + a[j] + a[k] + a[x]+a[y]+a[z]+" ";
if (str.indexOf(str1) == -1) {
count++;
str = str + str1;
}
//System.out.print(""+a[i] + a[j] + a[k] + a[x]+a[y]+a[z]+" ");
}
}
}
}
}
}
}
} System.out.println("总的排列数为:" + count);
System.out.print(str);
}
}
稍微改了下
import java.util.ArrayList;
import java.util.List;public class Test {
public static void main(String[] args) { for (int i = 111111; i <= 555555; i++) {
String s = String.valueOf(i);
List<Integer> l = new ArrayList<Integer>();
for(int m = 0;m<s.length();m++){
if(s.charAt(m) == '3'){
l.add(m);
}
}
if (s.charAt(2) == '4') {
continue;
}
boolean has = false;
for(int n = 0;n<l.size();n++){
int index_3 = l.get(n);
if (index_3 == 5 && s.charAt(index_3 - 1) == '5') {
has = true;
break;
} else if (index_3 == 0 && s.charAt(index_3 + 1) == '5') {
has = true;
break;
} else if (index_3 > 0 && index_3 < 5 && (s.charAt(index_3 - 1) == '5' || s.charAt(index_3 + 1) == '5')) {
has = true;
break;
} else {
has = false;
}
}
if(has == false){
System.out.println(i);
}
}
}
}
import java.text.*;public class NumberFormatTest
{
static boolean is2Digit(String src,String number)
{
int index = src.indexOf(number);
return src.indexOf(number,index+1)>-1;
}
public static void main(String[] args)
{
int nCounter = 0;
long start = new java.util.Date().getTime();
for (int i = 123345; i <= 543321; i++) {//全部可能范围
String s = String.valueOf(i);
//不能有0,6,7,8,9
if(s.indexOf("0")>-1||s.indexOf("6")>-1||s.indexOf("7")>-1||s.indexOf("8")>-1||s.indexOf("9")>-1)
continue;
//必须1,2,3,4,5都出现
if(s.indexOf("1")<0||s.indexOf("2")<0||s.indexOf("3")<0||s.indexOf("4")<0||s.indexOf("5")<0)
continue;
//至此还有以下可能情况:112345
if(is2Digit(s,"1")||is2Digit(s,"2")||is2Digit(s,"4")||is2Digit(s,"5"))
continue;
//至此,1245只出现1次,3必然出现2次
if (s.charAt(2) == '4')//4不在第三位
continue; //3和5不挨着
if (s.indexOf("35")>-1||s.indexOf("53")>-1)
continue;
//只有3可以有2次,其余只能出现1次
System.out.print(i+";");
nCounter ++;
}
System.out.println("\n"+nCounter);
long end = new java.util.Date().getTime();
System.out.println("Time:"+(end-start)+"ms");
}
}
123345;125433;132345;132543;133245;133254;133425;133452;143325;145233;152334;152
343;152433;213345;215433;231345;231543;233145;233154;233415;233451;243315;245133
;251334;251343;251433;312345;312543;313245;313254;313425;313452;315234;315243;31
5423;315432;321345;321543;323145;323154;323415;323451;325134;325143;325413;32543
1;331245;331254;331425;331452;331524;331542;332145;332154;332415;332451;332514;3
32541;341325;341523;342315;342513;343125;343152;343215;343251;345123;345132;3452
13;345231;413325;415233;423315;425133;431325;431523;432315;432513;433125;433152;
433215;433251;451233;451323;451332;452133;452313;452331;512334;512343;512433;513
234;513243;513324;513342;513423;513432;521334;521343;521433;523134;523143;523314
;523341;523413;523431;541233;541323;541332;542133;542313;542331;543123;543132;54
3213;543231;543312;543321;
118
Time:109ms
import java.util.HashSet;public class Test {
int[] array0 = { 1, 2, 3, 3, 4, 5 };
private HashSet<String> v; public Test() { v = new HashSet<String>();
long start = System.currentTimeMillis();
start(array0, "");
long last = (System.currentTimeMillis() - start);
System.out.println("共耗时" + last + "ms。" + "符合条件的数共" + v.size() + "个。");
for (Object obj : v) {
System.out.println(obj);
}
} private void start(int[] array, String alre) {
String temp = alre;
if (alre.length() == 6) {
v.add(alre);
return;
}
for (int i = 0; i < array.length; i++) {
if (alre.length() == 2 && array[i] == 4)
continue;
else if (alre.length() >= 1
&& alre.charAt(alre.length() - 1) == '3' && array[i] == 5)
continue;
else if (alre.length() >= 1
&& alre.charAt(alre.length() - 1) == '5' && array[i] == 3)
continue;
else {
alre += array[i] + "";
start(remove(array, i), alre);
}
alre = temp;
}
} private int[] remove(int[] nums, int index) {
int[] temp = new int[nums.length - 1];
int c = 0;
for (int i = 0; i < nums.length; i++) {
if (i == index)
continue;
else
temp[c++] = nums[i];
}
return temp;
} public static void main(String[] args) {
new Test();
}
}共耗时16ms。符合条件的数共118个。
315234
513432
521433
231345
315432
133254
341325
543213
323154
433215
451233
313254
331452
431325
213345
513243
315243
133452
233145
133245
543231
425133
523431
325413
233154
513234
313425
125433
231543
331245
133425
325431
343215
215433
123345
451323
541233
323451
331524
345213
331254
313452
143325
332451
145233
433251
323145
512334
331425
512433
413325
325143
342315
523341
451332
512343
342513
543132
542313
433152
332514
233451
432315
513342
423315
332415
325134
243315
345132
331542
345231
543312
542331
343125
543321
542133
432513
245133
452313
332145
345123
523413
152343
523134
541332
321543
433125
251343
523314
513324
332154
523143
521343
321345
152334
452133
431523
541323
315423
312345
343251
323415
132345
341523
233415
343152
251334
132543
332541
543123
313245
152433
415233
251433
513423
452331
521334
312543
import java.util.HashSet;public class Test {
int[] array0 = { 1, 2, 3, 3, 4, 5 };
private HashSet<String> v; public Test() { v = new HashSet<String>();
long start = System.currentTimeMillis();
start(array0, "");
long last = (System.currentTimeMillis() - start);
System.out.println("共耗时" + last + "ms。" + "符合条件的数共" + v.size() + "个。");
for (Object obj : v) {
System.out.println(obj);
}
} private void start(int[] array, String alre) {
String temp = alre;
int alreLength = alre.length();
int arrLength = array.length;
int count = alre.length() - 1;
if (alreLength == 6) {
v.add(alre);
return;
}
for (int i = 0; i < arrLength; i++) {
int arrTemp = array[i];
//这部分规则可以改
if (alreLength == 2 && arrTemp == 4)
continue;
else if (alreLength >= 1 && alre.charAt(count) == '3'
&& arrTemp == 5)
continue;
else if (alreLength >= 1 && alre.charAt(count) == '5'
&& arrTemp == 3)
continue;
else {
alre += arrTemp + "";
start(remove(array, i), alre);
}
alre = temp;
}
} private int[] remove(int[] nums, int index) {
int[] temp = new int[nums.length - 1];
int c = 0;
for (int i = 0; i < nums.length; i++) {
if (i == index)
continue;
else
temp[c++] = nums[i];
}
return temp;
} public static void main(String[] args) {
new Test();
}
}程序改了一点点,时间短了很多,楼主测试一下,看看符合要求不