日期:2014-05-20 浏览次数:20716 次
public static int getKind(int a[]){ return a.length*(a.length-1)*(a.length-2)/(3*2*1); } public static void main(String args[]) { System.out.println(getKind(new int[]{1,2,3,4,5,6,7,8,9}));// System.out.println(getKind(new int[9]));//跟里边的元素内容无关 }
------解决方案--------------------
public static void test(int m, int r) { int[] a = new int[10]; int i, j; i = 0; a[i] = 1; while (true) { if (a[i]-i<=m-r+1) { if (i==r-1) { for (j = 0; j<r; j++) System.out.printf("%4d", a[j]); System.out.printf("\n"); a[i]++; continue; } i++; a[i] = a[i-1]+1; } else { if (i==0) return; a[--i]++; } } } public static void main(String[] args) { test(9, 3); }
------解决方案--------------------
public class Test { private int count=0; public void getSqu(int a[], int index) { if(index>2) { count++; for(int i=0; i<3; i++) System.out.print(a[i]+" "); System.out.println(); return; } else { for(int i=1; i<=9; i++) { a[index]=i; if(check(a, index)) getSqu(a, index+1); a[index]=0; } } } public boolean check(int[] a, int index) { for(int i=0; i<index; i++) if(a[i]==a[index]) return false; return true; } public static void main(String[] args) { int[] a=new int[3]; Test test=new Test(); test.getSqu(a, 0); System.out.println("count="+test.count); } }