JAVA算法实现从000到999之间挑选出个十百三位不重复的数字
000-999一共1000个3位数字组合,需要从中取出数字,要求如下:
123,231,132,213,312,321这样的为一组,也就是3个同样的0-9之间的个位数组成的数字为一组,将000-999这1000个数字组合分组取出放入一个集合中请各位高手帮忙!~~在线等!~~
000-999一共1000个3位数字组合,需要从中取出数字,要求如下:
123,231,132,213,312,321这样的为一组,也就是3个同样的0-9之间的个位数组成的数字为一组,将000-999这1000个数字组合分组取出放入一个集合中请各位高手帮忙!~~在线等!~~
import java.util.ArrayList;
public Test{
public static void main(String args[]){
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
if(i==j)
break;
for(int k=1;k<=9;k++){
if(k==i||k==j)
break;
list.add(new Integer(100*i+10*j+k));
}
}
}
}
}
int all1, a, b, c; for (int i = 0; i < 1000; i++) {
all1 = i;
a = all1 / 100;
b = all1 % 100 / 10;
c = all1 % 10;
if (a != b && a != c && b != c) {
System.out.println(all1);
}
}
}
}
是这个吗
要求不单要a != b && a != c && b != c
出现a==c&&b==a&&c==b也不行
public static void main(String[] args){
String[] list=new String[1000];
for(int i=0;i<1000;i++){
if(i<10){
list[i]="00"+i;
}
else{
if(i<100){
list[i]="0"+i;
}else{
list[i]=""+i;
}
}
}
for(int i=0;i<1000;i++){
if((list[i].charAt(0)!=list[i].charAt(1))&&(
list[i].charAt(0)!=list[i].charAt(2))&&
(list[i].charAt(1)!=list[i].charAt(2))){
System.out.println(list[i]+" ");
}
}
}
}
[code]
import java.text.DecimalFormat;public class num
{
public static void main(String argv[])
{
DecimalFormat df = new DecimalFormat("000");
for (int i = 0; i < 10; i++)
for (int j = i + 1; j < 10; j++)
for (int k = j + 1; k < 10; k++)
{
System.out.print(df.format(i*100+j*10+k) + " ");
System.out.print(df.format(i*100+k*10+j) + " ");
System.out.print(df.format(j*100+i*10+k) + " ");
System.out.print(df.format(j*100+k*10+i) + " ");
System.out.print(df.format(k*100+i*10+j) + " ");
System.out.print(df.format(k*100+j*10+i) + "\n");
}
}
}
[/code]
{
public static void main(String argv[])
{
DecimalFormat df = new DecimalFormat("000");
for (int i = 0; i < 10; i++)
for (int j = i + 1; j < 10; j++)
for (int k = j + 1; k < 10; k++)
{
System.out.print(df.format(i*100+j*10+k) + " ");
System.out.print(df.format(i*100+k*10+j) + " ");
System.out.print(df.format(j*100+i*10+k) + " ");
System.out.print(df.format(j*100+k*10+i) + " ");
System.out.print(df.format(k*100+i*10+j) + " ");
System.out.print(df.format(k*100+j*10+i) + "\n");
}
}
}
升级看看。
public static void main(String[] args) {
String [] Total=new String [1000];
for(int i=0;i<1000;i++){
if(i<10){
Total[i]="00"+i;
}
else {
if(i<100){
Total[i]="0"+i;
}
else
Total[i]=i+"";
}
}
for(int i=0;i<1000;i++){
if(Total[i].charAt(0)!=Total[i].charAt(1)&&Total[i].charAt(1)!=Total[i].charAt(2)&&Total[i].charAt(2)!=Total[i].charAt(0)){
if((Total[i].contains("1"))&&(Total[i].contains("2")) && (Total[i].contains("3"))){
System.out.print(Total[i]+"\t");
}
}
}
}
}
不知道你是不是要这种效果
contains方法里参数好像只能是字符串啊 不能用循环变量代替 不知道的太多啊 哎
太意外了吧~绝对要举报!
String array[] = {"0","1","2","3","4","5","6","7","8","9"};
Object[] arr = subArraysByLength(array,3);/**
* 取得集合array的所有长度为eleNum的子集合
* 若eleNum<0则取得所有子集合
* @param array
* @param length
* @return
*/
public static Object[] subArraysByLength(Object[] array,int eleNum){
if (array == null){
return null;
}
int length = array.length;
List<Object[]> list = new ArrayList<Object[]>();
for (int i = 0; i < (1 << length); i ++){
if (!(eleNum < 0 || get1Cnt(i) == eleNum)){
continue;
}
List<Object> _list = new ArrayList<Object>();
for (int j = 0; j < length; j ++){
if((i&(1<<j)) != 0){
_list.add(array[j]);
}
}
list.add(_list.toArray(new Object[_list.size()]));
}
return list.toArray(new Object[list.size()]);
}
/**
* 取得二进制正整数中"1"的个数
* @param num
* @return
*/
private static int get1Cnt(int num){
int cnt = 0;
for (;num > 0;num >>= 1){
cnt += (num&1);
}
return cnt;
}
2,n个不同数全排列算法:
/**
* 对array中的元素全排列,结果以数组的形式保存在list中
* @param array
* @param list
*/
private static void getPerm(Object[] array,List<Object[]> list){
perm(array,0,array.length,list);
}
/**
* 用递归取排列数
* @param array
* @param m
* @param n
* @param list
*/
private static void perm(Object[] array,int m, int n,List<Object[]> list){
if (array == null){
list = new ArrayList<Object[]>();
return;
}
//用array的copy进行排列
Object[] _array = Arrays.copyOf(array, array.length);
if (m < n-1) {
perm(_array, m+1, n,list);
for (int i=m+1;i<n;i++) {
Object t=_array[m];
_array[m]=_array[i];
_array[i]=t;
perm(_array, m+1, n,list); t=_array[m];
_array[m]=_array[i];
_array[i]=t;
}
} else {
list.add(_array);
}
}
3,测试代码:
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
testDaffodil();
long t2 = System.currentTimeMillis();
System.out.println("一共用时" + (t2-t1) + "毫秒.");
}
public static void testDaffodil(){
String array[] = {"0","1","2","3","4","5","6","7","8","9"};
Object[] arr = subArraysByLength(array,3);
List<Object[]> list = new ArrayList<Object[]>();
for (int i = 0; i < arr.length; i ++){
List<Object[]> _list = new ArrayList<Object[]>();
Object[] _arr = (Object[])arr[i];//[1,2,3]
getPerm(_arr,_list);
list.add((Object[])_list.toArray(new Object[_list.size()]));
}
Object[] rt = (Object[])list.toArray(new Object[list.size()]);
System.out.println(Arrays.deepToString(rt));
}
4,测试结果:
......
[9, 3, 1], [9, 1, 3]], [[2, 3, 9], [2, 9, 3], [3, 2, 9], [3, 9, 2], [9, 3, 2], [9, 2, 3]], [[0, 4, 9], [0, 9, 4], [4, 0, 9], [4, 9, 0], [9, 4, 0], [9, 0, 4]], [[1, 4, 9], [1, 9, 4], [4, 1, 9], [4, 9, 1], [9, 4, 1], [9, 1, 4]], [[2, 4, 9], [2, 9, 4], [4, 2, 9], [4, 9, 2], [9, 4, 2], [9, 2, 4]], [[3, 4, 9], [3, 9, 4], [4, 3, 9], [4, 9, 3], [9, 4, 3], [9, 3, 4]], [[0, 5, 9], [0, 9, 5], [5, 0, 9], [5, 9, 0], [9, 5, 0], [9, 0, 5]], [[1, 5, 9], [1, 9, 5], [5, 1, 9], [5, 9, 1], [9, 5, 1], [9, 1, 5]], [[2, 5, 9], [2, 9, 5], [5, 2, 9], [5, 9, 2], [9, 5, 2], [9, 2, 5]], [[3, 5, 9], [3, 9, 5], [5, 3, 9], [5, 9, 3], [9, 5, 3], [9, 3, 5]], [[4, 5, 9], [4, 9, 5], [5, 4, 9], [5, 9, 4], [9, 5, 4], [9, 4, 5]], [[0, 6, 9], [0, 9, 6], [6, 0, 9], [6, 9, 0], [9, 6, 0], [9, 0, 6]], [[1, 6, 9], [1, 9, 6], [6, 1, 9], [6, 9, 1], [9, 6, 1], [9, 1, 6]], [[2, 6, 9], [2, 9, 6], [6, 2, 9], [6, 9, 2], [9, 6, 2], [9, 2, 6]], [[3, 6, 9], [3, 9, 6], [6, 3, 9], [6, 9, 3], [9, 6, 3], [9, 3, 6]], [[4, 6, 9], [4, 9, 6], [6, 4, 9], [6, 9, 4], [9, 6, 4], [9, 4, 6]], [[5, 6, 9], [5, 9, 6], [6, 5, 9], [6, 9, 5], [9, 6, 5], [9, 5, 6]], [[0, 7, 9], [0, 9, 7], [7, 0, 9], [7, 9, 0], [9, 7, 0], [9, 0, 7]], [[1, 7, 9], [1, 9, 7], [7, 1, 9], [7, 9, 1], [9, 7, 1], [9, 1, 7]], [[2, 7, 9], [2, 9, 7], [7, 2, 9], [7, 9, 2], [9, 7, 2], [9, 2, 7]], [[3, 7, 9], [3, 9, 7], [7, 3, 9], [7, 9, 3], [9, 7, 3], [9, 3, 7]], [[4, 7, 9], [4, 9, 7], [7, 4, 9], [7, 9, 4], [9, 7, 4], [9, 4, 7]], [[5, 7, 9], [5, 9, 7], [7, 5, 9], [7, 9, 5], [9, 7, 5], [9, 5, 7]], [[6, 7, 9], [6, 9, 7], [7, 6, 9], [7, 9, 6], [9, 7, 6], [9, 6, 7]], [[0, 8, 9], [0, 9, 8], [8, 0, 9], [8, 9, 0], [9, 8, 0], [9, 0, 8]], [[1, 8, 9], [1, 9, 8], [8, 1, 9], [8, 9, 1], [9, 8, 1], [9, 1, 8]], [[2, 8, 9], [2, 9, 8], [8, 2, 9], [8, 9, 2], [9, 8, 2], [9, 2, 8]], [[3, 8, 9], [3, 9, 8], [8, 3, 9], [8, 9, 3], [9, 8, 3], [9, 3, 8]], [[4, 8, 9], [4, 9, 8], [8, 4, 9], [8, 9, 4], [9, 8, 4], [9, 4, 8]], [[5, 8, 9], [5, 9, 8], [8, 5, 9], [8, 9, 5], [9, 8, 5], [9, 5, 8]], [[6, 8, 9], [6, 9, 8], [8, 6, 9], [8, 9, 6], [9, 8, 6], [9, 6, 8]], [[7, 8, 9], [7, 9, 8], [8, 7, 9], [8, 9, 7], [9, 8, 7], [9, 7, 8]]]
一共用时16毫秒.5,注意:
不要尝试用这个递归算法对较多的元素排列,比如10个元素全排
这样会内存溢出的~
//10*9*8/(3*2*1) = 120
private static void getCollection(){ ArrayList al = new ArrayList();
ArrayList alParam = new ArrayList();
for(int i=123;i<=999;i++){
int one = i/100;
int two = (i-one*100)/10;
int thr = i%10;
if(one != two && two != thr && thr != one){
String[] arr = (i+"").split("");
Collections.sort(Arrays.asList(arr));
String str = "";
for(int j=1;j<arr.length;j++) str += arr[j];
int index = alParam.indexOf(str);
if(index == -1){
alParam.add(str);
index = alParam.indexOf(str);
}
if(al.size() < index+1)
al.add(new ArrayList());
((ArrayList)al.get(index)).add(i+"");
}
} System.out.println(al.size());
System.out.println(al);
}
------------
你用的1.4吧.
package test;
public class TestClass {
public static void main(String[] args) {
TestClass testClass = new TestClass();
testClass.findNumber();
}
/**
* 关键,利用3个数的排列,找到一个数,肯定就找到另一个5个数了,所以不需要每个都找
* 比如找到123,则123,132,213,231,312,321这六个数都一定符合要求了
* 关于3个数不能相同,其实也不需要特意的去进行比较,
* 利用上面的特点,我们找这六个数中特征比较明显的一个即可,那就是百位,十位,个位递增的那个数
* 也就是百位<十位 并且 十位 < 个位 ,这样,隐含的,就满足了题目的个位十位百位不相同的要求了
* 这样,循环的次数就相当的少
*/
public void findNumber(){
int groupNum = 0;
for(int i = 0;i <= 9;i++){
for(int j = i+1 ; j <= 9;j++){
for(int k = j+1;k <=9;k++){
groupNum++;
printNumber(groupNum,i,j,k);
}
}
}
System.out.println("一共分为"+ groupNum + "组,每组六个数");
}
public void printNumber(int groupNum,int i ,int j,int k){
System.out.println("第" + groupNum + "组:");
if( i == 0) System.out.print("0");
System.out.print(i * 100 + j * 10 + k + " ");
if( i == 0) System.out.print("0");
System.out.print(i * 100 + k* 10 + j+ " ");
if( j == 0) System.out.print("0");
System.out.print(j * 100 + i* 10 + k+ " ");
if( j == 0) System.out.print("0");
System.out.print(j * 100 + k* 10 + i+ " ");
if( k == 0) System.out.print("0");
System.out.print(k * 100 + i* 10 + j+ " ");
if( k == 0) System.out.print("0");
System.out.print(k * 100 + j* 10 + i+ " ");
System.out.println();
}}结果:
第1组:
012 021 102 120 201 210
第2组:
013 031 103 130 301 310
第3组:
014 041 104 140 401 410
第4组:
015 051 105 150 501 510
第5组:
016 061 106 160 601 610
第6组:
017 071 107 170 701 710
第7组:
018 081 108 180 801 810
第8组:
019 091 109 190 901 910
第9组:
023 032 203 230 302 320
第10组:
024 042 204 240 402 420
第11组:
025 052 205 250 502 520
第12组:
026 062 206 260 602 620
第13组:
027 072 207 270 702 720
第14组:
028 082 208 280 802 820
第15组:
029 092 209 290 902 920
第16组:
034 043 304 340 403 430
第17组:
035 053 305 350 503 530
第18组:
036 063 306 360 603 630
第19组:
037 073 307 370 703 730
第20组:
038 083 308 380 803 830
第21组:
039 093 309 390 903 930
第22组:
045 054 405 450 504 540
第23组:
046 064 406 460 604 640
第24组:
047 074 407 470 704 740
第25组:
048 084 408 480 804 840
第26组:
049 094 409 490 904 940
第27组:
056 065 506 560 605 650
第28组:
057 075 507 570 705 750
第29组:
058 085 508 580 805 850
第30组:
059 095 509 590 905 950
第31组:
067 076 607 670 706 760
第32组:
068 086 608 680 806 860
第33组:
069 096 609 690 906 960
第34组:
078 087 708 780 807 870
第35组:
079 097 709 790 907 970
第36组:
089 098 809 890 908 980
第37组:
123 132 213 231 312 321
第38组:
124 142 214 241 412 421
第39组:
125 152 215 251 512 521
第40组:
126 162 216 261 612 621
第41组:
127 172 217 271 712 721
第42组:
128 182 218 281 812 821
第43组:
129 192 219 291 912 921
第44组:
134 143 314 341 413 431
第45组:
135 153 315 351 513 531
第46组:
136 163 316 361 613 631
第47组:
137 173 317 371 713 731
第48组:
138 183 318 381 813 831
第49组:
139 193 319 391 913 931
第50组:
145 154 415 451 514 541
第51组:
146 164 416 461 614 641
第52组:
147 174 417 471 714 741
第53组:
148 184 418 481 814 841
第54组:
149 194 419 491 914 941
第55组:
156 165 516 561 615 651
第56组:
157 175 517 571 715 751
第57组:
158 185 518 581 815 851
第58组:
159 195 519 591 915 951
第59组:
167 176 617 671 716 761
第60组:
168 186 618 681 816 861
第61组:
169 196 619 691 916 961
第62组:
178 187 718 781 817 871
第63组:
179 197 719 791 917 971
第64组:
189 198 819 891 918 981
第65组:
234 243 324 342 423 432
第66组:
235 253 325 352 523 532
第67组:
236 263 326 362 623 632
第68组:
237 273 327 372 723 732
第69组:
238 283 328 382 823 832
第70组:
239 293 329 392 923 932
第71组:
245 254 425 452 524 542
第72组:
246 264 426 462 624 642
第73组:
247 274 427 472 724 742
第74组:
248 284 428 482 824 842
第75组:
249 294 429 492 924 942
第76组:
256 265 526 562 625 652
第77组:
257 275 527 572 725 752
第78组:
258 285 528 582 825 852
第79组:
259 295 529 592 925 952
第80组:
267 276 627 672 726 762
第81组:
268 286 628 682 826 862
第82组:
269 296 629 692 926 962
第83组:
278 287 728 782 827 872
第84组:
279 297 729 792 927 972
第85组:
289 298 829 892 928 982
第86组:
345 354 435 453 534 543
第87组:
346 364 436 463 634 643
第88组:
347 374 437 473 734 743
第89组:
348 384 438 483 834 843
第90组:
349 394 439 493 934 943
第91组:
356 365 536 563 635 653
第92组:
357 375 537 573 735 753
第93组:
358 385 538 583 835 853
第94组:
359 395 539 593 935 953
第95组:
367 376 637 673 736 763
第96组:
368 386 638 683 836 863
第97组:
369 396 639 693 936 963
第98组:
378 387 738 783 837 873
第99组:
379 397 739 793 937 973
第100组:
389 398 839 893 938 983
第101组:
456 465 546 564 645 654
第102组:
457 475 547 574 745 754
第103组:
458 485 548 584 845 854
第104组:
459 495 549 594 945 954
第105组:
467 476 647 674 746 764
第106组:
468 486 648 684 846 864
第107组:
469 496 649 694 946 964
第108组:
478 487 748 784 847 874
第109组:
479 497 749 794 947 974
第110组:
489 498 849 894 948 984
第111组:
567 576 657 675 756 765
第112组:
568 586 658 685 856 865
第113组:
569 596 659 695 956 965
第114组:
578 587 758 785 857 875
第115组:
579 597 759 795 957 975
第116组:
589 598 859 895 958 985
第117组:
678 687 768 786 867 876
第118组:
679 697 769 796 967 976
第119组:
689 698 869 896 968 986
第120组:
789 798 879 897 978 987
一共分为120组,每组六个数
static void main(String[] s){
int maxColumn=3; // 最大列
int maxRow=10; //最大行
int[] a=new int[maxRow];
for(int i=0;i<maxRow;i++)a[i]=i; //初始化每行可能出现的值
print(maxColumn,a,"");
System.out.println("Total:"+total);
}
static int iNull=-1; // 占用标志
static int total=0;
static void print(int column,int[] a,String value){
for(int i=0;i<a.length;i++){
int temp=a[i];
if(temp!=iNull){
a[i]=iNull;//标志已用
if(column==1){
Systwm.out.print(value+temp+" ");
total++;
}else{
print(column-1,a,value+temp);
}
a[i]=i;
}
}
if(column!=1)System.out.println();
} 用递归 计算任意列 行
没执行不知道可用不
重复三次就得出要求了
这个算法的时间复杂度(排出随机数的计算)等于O(n),空间也一样,都是线形的,符合效率void main( ){
StringBuffer sb = new StringBuffer("0123456789");
String str = "";
for( int i = 0; i < 3; i++ ){
int index = (int)Math.random() * sb.length() ;
char ch = sb.charAt( index );
sb = sb.deleteChatAt( index );
str += ch;
}
System.out.println(str);
}
public static void main(String[] args){
int[] read=new int[1000];
int n=0;
for(int i=0;i<=9;i++){
for(int j=0;j<=9;j++){
if(i==j){
continue;
}
for(int k=0;k<=9;k++){
if(k==i||k==j||read[i*100+j*10+k]==1){
continue;
}
System.out.print(i);
System.out.print(j);
System.out.print(k+" ");
System.out.print(i);
System.out.print(k);
System.out.print(j+" ");
System.out.print(j);
System.out.print(i);
System.out.print(k+" ");
System.out.print(j);
System.out.print(k);
System.out.print(i+" ");
System.out.print(k);
System.out.print(i);
System.out.print(j+" ");
System.out.print(k);
System.out.print(j);
System.out.print(i+" ");
System.out.println("第"+(++n)+"组");
read[i*100+j*10+k]=1;
read[i*100+k*10+j]=1;
read[j*100+i*10+k]=1;
read[j*100+k*10+i]=1;
read[k*100+i*10+j]=1;
read[k*100+j*10+i]=1;
}
}
}
}}
弱弱的发一个,有错误的话请赐教
小菜鸟飘过PS:运行结果:
012 021 102 120 201 210 第1组
013 031 103 130 301 310 第2组
014 041 104 140 401 410 第3组
015 051 105 150 501 510 第4组
016 061 106 160 601 610 第5组
017 071 107 170 701 710 第6组
018 081 108 180 801 810 第7组
019 091 109 190 901 910 第8组
023 032 203 230 302 320 第9组
024 042 204 240 402 420 第10组
025 052 205 250 502 520 第11组
026 062 206 260 602 620 第12组
027 072 207 270 702 720 第13组
028 082 208 280 802 820 第14组
029 092 209 290 902 920 第15组
034 043 304 340 403 430 第16组
035 053 305 350 503 530 第17组
036 063 306 360 603 630 第18组
037 073 307 370 703 730 第19组
038 083 308 380 803 830 第20组
039 093 309 390 903 930 第21组
045 054 405 450 504 540 第22组
046 064 406 460 604 640 第23组
047 074 407 470 704 740 第24组
048 084 408 480 804 840 第25组
049 094 409 490 904 940 第26组
056 065 506 560 605 650 第27组
057 075 507 570 705 750 第28组
058 085 508 580 805 850 第29组
059 095 509 590 905 950 第30组
067 076 607 670 706 760 第31组
068 086 608 680 806 860 第32组
069 096 609 690 906 960 第33组
078 087 708 780 807 870 第34组
079 097 709 790 907 970 第35组
089 098 809 890 908 980 第36组
123 132 213 231 312 321 第37组
124 142 214 241 412 421 第38组
125 152 215 251 512 521 第39组
126 162 216 261 612 621 第40组
127 172 217 271 712 721 第41组
128 182 218 281 812 821 第42组
129 192 219 291 912 921 第43组
134 143 314 341 413 431 第44组
135 153 315 351 513 531 第45组
136 163 316 361 613 631 第46组
137 173 317 371 713 731 第47组
138 183 318 381 813 831 第48组
139 193 319 391 913 931 第49组
145 154 415 451 514 541 第50组
146 164 416 461 614 641 第51组
147 174 417 471 714 741 第52组
148 184 418 481 814 841 第53组
149 194 419 491 914 941 第54组
156 165 516 561 615 651 第55组
157 175 517 571 715 751 第56组
158 185 518 581 815 851 第57组
159 195 519 591 915 951 第58组
167 176 617 671 716 761 第59组
168 186 618 681 816 861 第60组
169 196 619 691 916 961 第61组
178 187 718 781 817 871 第62组
179 197 719 791 917 971 第63组
189 198 819 891 918 981 第64组
234 243 324 342 423 432 第65组
235 253 325 352 523 532 第66组
236 263 326 362 623 632 第67组
237 273 327 372 723 732 第68组
238 283 328 382 823 832 第69组
239 293 329 392 923 932 第70组
245 254 425 452 524 542 第71组
246 264 426 462 624 642 第72组
247 274 427 472 724 742 第73组
248 284 428 482 824 842 第74组
249 294 429 492 924 942 第75组
256 265 526 562 625 652 第76组
257 275 527 572 725 752 第77组
258 285 528 582 825 852 第78组
259 295 529 592 925 952 第79组
267 276 627 672 726 762 第80组
268 286 628 682 826 862 第81组
269 296 629 692 926 962 第82组
278 287 728 782 827 872 第83组
279 297 729 792 927 972 第84组
289 298 829 892 928 982 第85组
345 354 435 453 534 543 第86组
346 364 436 463 634 643 第87组
347 374 437 473 734 743 第88组
348 384 438 483 834 843 第89组
349 394 439 493 934 943 第90组
356 365 536 563 635 653 第91组
357 375 537 573 735 753 第92组
358 385 538 583 835 853 第93组
359 395 539 593 935 953 第94组
367 376 637 673 736 763 第95组
368 386 638 683 836 863 第96组
369 396 639 693 936 963 第97组
378 387 738 783 837 873 第98组
379 397 739 793 937 973 第99组
389 398 839 893 938 983 第100组
456 465 546 564 645 654 第101组
457 475 547 574 745 754 第102组
458 485 548 584 845 854 第103组
459 495 549 594 945 954 第104组
467 476 647 674 746 764 第105组
468 486 648 684 846 864 第106组
469 496 649 694 946 964 第107组
478 487 748 784 847 874 第108组
479 497 749 794 947 974 第109组
489 498 849 894 948 984 第110组
567 576 657 675 756 765 第111组
568 586 658 685 856 865 第112组
569 596 659 695 956 965 第113组
578 587 758 785 857 875 第114组
579 597 759 795 957 975 第115组
589 598 859 895 958 985 第116组
678 687 768 786 867 876 第117组
679 697 769 796 967 976 第118组
689 698 869 896 968 986 第119组
789 798 879 897 978 987 第120组
for(int i=0;i<=9;i++)
for(int j=i+1;j<=9;j++)
for(int k=j+1;k<=9;k++){
int[] a = new int[]{
(100*i+10*j+k),(100*i+10*k+j),
(100*j+10*i+k),(100*j+10*k+i),
(100*k+10*i+j),(100*k+10*j+i)};
for(int ix = 0; ix < a.length; ++ix)
System.out.print(a[ix]+((ix != a.length-1)?",":"\n"));
}
import java.util.*;class test2{
int[] list;
StringBuffer sb = new StringBuffer();
int start;
public test2(int[] in){
list = in;
printList();
System.out.print(sb);
}
void replaceStr(int c1,int c2){
int temp = list[c1];
list[c1] = list[c2];
list[c2] = temp;
}
void printList(){
if(start==list.length-1)
for(int i=0;i<list.length;i++)
sb.append(list[i]+(i==list.length-1?" ":","));
else
for(int i=start;i<list.length;i++){
replaceStr(start++,i);
printList();
replaceStr(i,--start);
}
}
}class test1{
public List list = new ArrayList();
public void run(int n,int r,int s[]){
for(int i=n;i>=r;i--){
int tmp[] = s.clone();
tmp[r-1] = i;
if(r==1)
list.add(tmp);
if(r>1)
run(i-1,r-1,tmp);
}
}
}public class test{
test(){
long l1 = System.currentTimeMillis();
test1 t1 = new test1();
t1.run(10,3,new int[3]);
for(Object o:t1.list)
new test2((int[])o);
long l2 = System.currentTimeMillis();
System.out.println(l2-l1);
}
public static void main(String args[]){
new test();
}
}
测试为20毫秒左右
C:\java>java test
8,9,10 8,10,9 9,8,10 9,10,8 10,9,8 10,8,9 7,9,10 7,10,9 9,7,10 9,10,7 10,9,7 10,
7,9 6,9,10 6,10,9 9,6,10 9,10,6 10,9,6 10,6,9 5,9,10 5,10,9 9,5,10 9,10,5 10,9,5
10,5,9 4,9,10 4,10,9 9,4,10 9,10,4 10,9,4 10,4,9 3,9,10 3,10,9 9,3,10 9,10,3 10
,9,3 10,3,9 2,9,10 2,10,9 9,2,10 9,10,2 10,9,2 10,2,9 1,9,10 1,10,9 9,1,10 9,10,
1 10,9,1 10,1,9 7,8,10 7,10,8 8,7,10 8,10,7 10,8,7 10,7,8 6,8,10 6,10,8 8,6,10 8
,10,6 10,8,6 10,6,8 5,8,10 5,10,8 8,5,10 8,10,5 10,8,5 10,5,8 4,8,10 4,10,8 8,4,
10 8,10,4 10,8,4 10,4,8 3,8,10 3,10,8 8,3,10 8,10,3 10,8,3 10,3,8 2,8,10 2,10,8
8,2,10 8,10,2 10,8,2 10,2,8 1,8,10 1,10,8 8,1,10 8,10,1 10,8,1 10,1,8 6,7,10 6,1
0,7 7,6,10 7,10,6 10,7,6 10,6,7 5,7,10 5,10,7 7,5,10 7,10,5 10,7,5 10,5,7 4,7,10
4,10,7 7,4,10 7,10,4 10,7,4 10,4,7 3,7,10 3,10,7 7,3,10 7,10,3 10,7,3 10,3,7 2,
7,10 2,10,7 7,2,10 7,10,2 10,7,2 10,2,7 1,7,10 1,10,7 7,1,10 7,10,1 10,7,1 10,1,
7 5,6,10 5,10,6 6,5,10 6,10,5 10,6,5 10,5,6 4,6,10 4,10,6 6,4,10 6,10,4 10,6,4 1
0,4,6 3,6,10 3,10,6 6,3,10 6,10,3 10,6,3 10,3,6 2,6,10 2,10,6 6,2,10 6,10,2 10,6
,2 10,2,6 1,6,10 1,10,6 6,1,10 6,10,1 10,6,1 10,1,6 4,5,10 4,10,5 5,4,10 5,10,4
10,5,4 10,4,5 3,5,10 3,10,5 5,3,10 5,10,3 10,5,3 10,3,5 2,5,10 2,10,5 5,2,10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