首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在一个数字中没有重复数字的计数

在一个数字中没有重复数字的计数
EN

Stack Overflow用户
提问于 2016-04-21 16:07:03
回答 2查看 1.4K关注 0票数 2

我正在优化我的程序计数器。它取决于数字的大小(从3位到10位,没有重复)- i.g。012,013,214等。我的第一个解决方案是循环,如下所示:

代码语言:javascript
复制
private void sample() {
        int[] varValue = new int[3];
        innerloop: for (int a = 0; a < 10; a++) {
            for (int b = 0; b < 10; b++) {
                if (a == b)
                    continue;
                for (int c = 0; c < 10; c++) {
                    if (c == b)
                        continue;
                    if (c == a)
                        continue;
                    varValue[0] = a;
                    varValue[1] = b;
                    varValue[2] = c;

                    int EqOne = varValue[0] * 100 + varValue[1] * 10 + varValue[2];

                    if (EqOne == 432) {
                        System.out.println(varValue[0]);
                        System.out.println(varValue[1]);
                        System.out.println(varValue[2]);
                        break innerloop;
                    }

                }
            }

        }
    }

或10位数(https://gist.github.com/TrollerN/6a0e470c539c57fd4cd73086cf6eb41b)

然后,我可以将这些a、b、c添加到int[]或ArrayList中,并使用它。这是可以的,但对于10个不同的数字,它用于循环的if状态+我必须创建8种不同的方法-每个数字的数字一个。

我还试图创建10位数字的ArrayList (0,1,2,3,4,5,6,7,8,9),对它进行洗牌,然后返回3-10位数,但是它可以为9-10位数做无休止的循环。

我的下一个“很棒”的想法(诚实地说,其中一个是迄今为止最愚蠢的:D)是将组合的ArrayList保存到文件中,为每一种情况,然后加载所需的一个,并通过它来找到解决方案。

我正在考虑创建一些方法,它将获取数字数(“因此它知道它需要多少个循环”)和/或最后一个解决方案,这样它就知道要开始了。

编辑:我用示例方法修改了这个问题。当然,int EqOne和如果状态要复杂得多,但它在技术上显示了我想要实现的目标--至少我希望:

我正在寻找的是一个方法,它将创建尽可能多的循环和大数组/数组列表?

EN

回答 2

Stack Overflow用户

发布于 2016-04-22 03:05:46

假设您需要给定长度的数字,而不是字符串,一种方法是使用boolean[10]记住当前正在使用的数字,这样就可以快速跳过这些数字。

然后将数字保存在数组中,并像正常数字一样增加最后一个数字。当它翻滚时,增加第二个最后一个数字,以此类推,将尾随数字重置为未使用的数字。

示例:如果当前数字为1097,则如下所示:

代码语言:javascript
复制
Digits  In Use               Description
1097    0 1 _ _ _ _ _ 7 _ 9
1097    0 1 _ _ _ _ _ _ _ 9  Clear in-use of last digit
1098    0 1 _ _ _ _ _ _ _ 9  Increment last digit
1098    0 1 _ _ _ _ _ _ 8 9  Mark in-use
=======================================================
1098    0 1 _ _ _ _ _ _ _ 9  Clear in-use of last digit
1099    0 1 _ _ _ _ _ _ _ 9  Increment last digit, but it's in use
109?    0 1 _ _ _ _ _ _ _ 9  Rollover, so go to previous digit
109?    0 1 _ _ _ _ _ _ _ _  Clear in-use
10??    0 1 _ _ _ _ _ _ _ _  Rollover, so go to previous digit
10??    _ 1 _ _ _ _ _ _ _ _  Clear in-use
11??    _ 1 _ _ _ _ _ _ _ _  Increment digit, but it's in use
12??    _ 1 _ _ _ _ _ _ _ _  Increment digit
12??    _ 1 2 _ _ _ _ _ _ _  Mark in-use
120?    0 1 2 _ _ _ _ _ _ _  Set to first unused digit, and mark in-use
1203    0 1 2 3 _ _ _ _ _ _  Set to first unused digit, and mark in-use
=======================================================

如您所见,逻辑使它从109710981203

这就是逻辑所在。有关运行示例,请参见依佩恩

代码语言:javascript
复制
final class UniqueDigitCounter {
    private int[]     digits;
    private boolean[] inUse = new boolean[10];
    public UniqueDigitCounter(int digitCount) {
        if (digitCount < 1 || digitCount > 10)
            throw new IllegalArgumentException("Invalid digit count: " + digitCount);
        this.digits = new int[digitCount];
        for (int i = 0; i < digitCount; i++) {
            this.digits[i] = i;
            this.inUse[i] = true;
        }
    }
    public long next() {
        if (this.digits == null)
            return -1; // end of sequence
        long value = 0;
        for (int i = 0; i < this.digits.length; i++)
            value = value * 10 + this.digits[i];
        for (int i = this.digits.length - 1; ; i--) {
            if (i == -1) {
                this.digits = null; // end of sequence
                break;
            }
            int digit = this.digits[i];
            this.inUse[digit] = false;
            if ((digit = nextDigit(digit + 1)) != -1) {
                this.digits[i] = digit;
                while (++i < this.digits.length)
                    this.digits[i] = nextDigit(0);
                break;
            }
        }
        return value;
    }
    private int nextDigit(int minDigit) {
        for (int digit = minDigit; digit < 10; digit++)
            if (! this.inUse[digit]) {
                this.inUse[digit] = true;
                return digit;
            }
        return -1;
    }
}
票数 2
EN

Stack Overflow用户

发布于 2016-04-22 02:48:43

因为你不允许重复数字,所以很难直接生成你想要的列表。我认为最简单的方法是首先得到所有的n长度组合,然后得到每个组合的所有排列。也就是说,对于n = 2情况,您将生成"01", "02", ... , "78", "79", "89",然后得到其中每一个的所有排列。

下面是我编的一个快速程序,可以帮你找到你想要的东西:

代码语言:javascript
复制
public static List<String> getCombinations (String digits, int n) {

  List<String> out = new ArrayList<String>();
  for ( int k = 0; k < digits.length (); ++k ) {
    if ( n == 1 )
      out.add (digits.charAt (k) + "");
    else {
      for ( String s : getCombinations (digits.substring (k + 1), n - 1) ) {
        out.add (digits.charAt (k) + s);
      }
    }
  }

  return out;
}


public static List<String> getPermutations (String s, String prefix) {

  List<String> out = new ArrayList<String>();

  if ( s.length () == 1 ) {
    out.add (prefix + s);
  }
  else {
    for ( int i = 0; i < s.length (); ++i ) {
      out.addAll (getPermutations (prefix + s.charAt (i), s.substring (0, i) + s.substring (i + 1)));
    }
  }

  return out;
}


public static List<String> getPermutations (List<String> combinations) {
  List<String> out = new ArrayList<String>();
  for ( String s : combinations )
    out.addAll (getPermutations (s, ""));

  return out;
}


public static void main (String[] args) {

  List<String> combinations = getCombinations ("0123456789", 3);

  List<String> permutations = getPermutations (combinations);

  for ( String s : permutations ) {
    System.out.println (s);
  }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36775057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档