用java语言编程:有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

你好,打扰了,网上可以查到这个问题的答案,很不错。我现在想把这个实现用一个方法封装起来,方法有两个参数,一个int数组,一个生成的数字位数(比如三位数),不知道该怎么操作,麻烦了,能不能帮个忙,辛苦辛苦!

int i=0;  //保存百位上的数
int j=0;  //保存十位上的数
int k=0;  //保存各位上的数
int t=0;  //保存数字个数 
for(i=1;i<=4;i++){
   for(j=1;j<=4;j++){
   for(k=1;k<=4;k++){
if(i!=j && j!=k && i!=k){
     t+=1;
System.out.println(i*100+j*10+k);

     }  
   }
 }
System.out.println (t);
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-03-24
public class HundredNumber{
private int[] a={1, 2, 3, 4};//定义数组并赋值
private int num=0;//定义组成三位数的个数
private int hundredNum=0;//定义组成的三位数
//hundred-百位, tens-十位, units-个位
public int hundNumber(int hundred, int tens, int units){
return 100*hundred+10*tens+1*units;
}
public static void main(String[] args){
HundredNumber hn=new HundredNumber();
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int m=0; m<4; m++){
if(hn.a[i]!=hn.a[j]&&hn.a[j]!=hn.a[m]&&hn.a[m]!=hn.a[i]){
hn.hundredNum=hn.hundNumber(hn.a[i], hn.a[j],hn.a[m]);
hn.num++;
System.out.println(hn.hundredNum);
}
}
}
}
System.out.println("Total: "+hn.num);
}
第2个回答  2017-12-28
@Test
public void test(){
    int[] a = {1,2,3,4};
    int n = 3;
    //取出n位不同的数字,就能保证位置上不重复,
    //题目中没有限制n的大小,最好的方式用递归
    fun(0,new int[n],a);
}

/**
 *
 * @param x 当前的取第几位
 * @param a 取出n位数组
 * @param b 选取参数的数组(这里可以优化的,可以用list类里面去掉一个参数,那么递归的效率会提高)
 */
public void fun(int x,int[] a,int[] b){
  if(x<a.length){//设置跳出递归的条件
        for(int i=0;i<b.length;i++){
            int j=0;
            boolean f = true;
            while(j<x){
                if(a[j]==b[i]){
                    f=false;
                    break;
                }
                j++;
            }
            a[x]=b[i];
            if(f&&x==a.length-1){
                System.out.println(Arrays.toString(a));//输出所有的组合,也可以用一个参数接收,自己定义
            }else if(f){//如果不是最后一位,那么继续递归去取数
                fun(x+1,a,b);
            }
        }
    }
}

第3个回答  2017-03-30
            int[] array = { 1, 2, 3, 4 };
            List<int> result = new List<int>();
            foreach (int i in array)
            {
                int h = i * 100;
                foreach (int ii in array)
                {
                    if (ii != i)
                    {
                        int hh = h + ii * 10;
                        foreach (int iii in array)
                        {
                            if (iii != ii && iii != i) 
                            {
                                int hhhh = hh + iii;
                                result.Add(hhhh);
                            }
                        }
                    }
                }
            }
            textBox1.Text += "总数为:" + result.Count.ToString() + "\r\n各项值为:\r\n";
            foreach(int rs in result)
            {
                textBox1.Text += rs.ToString() + "\r\n";
            }

数学学得不好,所以不保证是最高效率的方法,另外,自己拖多行文本框进去。

第4个回答  2014-05-14

按题意,其实就是4取3的排列问题,共有24个数:

>> p=perms(1:4);
>> sum(p(:,1:3).*repmat([100 10 1],size(p,1),1),2)
ans =
   432
   431
   423
   421
   412
   413
   342
   341
   324
   321
   312
   314
   234
   231
   243
   241
   214
   213
   132
   134
   123
   124
   142
   143

相似回答