【问题标题】:how to display all the distinct 7-digit number among 2 to 9?如何显示2到9中所有不同的7位数字?
【发布时间】:2010-12-09 08:01:51
【问题描述】:

我的意思是所有数字 _ _ _ _ _ _ _ 都是不同的,只能输入 2 到 9 的值。我试过使用数组和循环,但我就是想不出显示所有数字的解决方案。 示例 2 到 9 中的 7 位数字是: 234567 234568 234569

324567 324568 324569

请注意,这些值中没有一个数字是重复的。 我真的不知道发生了什么。 请帮帮我!

【问题讨论】:

  • 您要查找的关键字是“排列”。
  • 我喜欢你所有的 7 位数字示例都有 6 位数字......
  • 我避免在没有尝试代码的情况下回答家庭作业问题。只是不想鼓励懒惰的学生。我有种感觉,就是这个。无论这是否是作业,发布您尝试过的代码,我们可以从那里开始。我认为这对你会更有用。
  • 当有 no 指示时,我认为添加作业标签的形式很糟糕。过去有很多简单的自我教育的例子。
  • 是的,我在评论后看到了,然后放回去了。抱歉。不过,除非 OP 声明,否则不应放入作业标签。

标签: c distinct-values


【解决方案1】:

因为这听起来有点像家庭作业,所以这里的教育将是一个循序渐进的过程。首先,尝试蛮力。这是一个可以做到的算法(嗯,真的是 Python):

for n1 in range(2,10):
  for n2 in range(2,10):
    if n2 != n1:
      for n3 in range(2,10):
        if n3 != n2 and n3 != n1:
          for n4 in range(2,10):
            if n4 != n3 and n4 != n2 and n4 != n1:
              for n5 in range(2,10):
                if n5 != n4 and n5 != n3 and n5 != n2 and n5 != n1:
                  for n6 in range(2,10):
                    if n6 != n5 and n6 != n4 and n6 != n3 and n6 != n2 and n6 != n1:
                      for n7 in range(2,10):
                        if n7 != n6 and n7 != n5 and n7 != n4 and n7 != n3 and n7 != n2 and n7 != n1:
                          print "%d%d%d%d%d%d%d"%(n1,n2,n3,n4,n5,n6,n7)

它基本上是七个嵌套循环,每个数字位置一个,检查任何时候都没有重复。请注意,这不是一个可以很好地扩展到 很多 个数字位置的解决方案,但它在其中七个位置上表现得非常好。如果您想要更多,那么使用较少蛮力的解决方案会更好。

【讨论】:

  • 感谢您的教导。我认为这对我有用..我现在正在尝试
  • 在 C 中做同样的事情应该不会太难。我不想发布 C 代码以防万一它是家庭作业,因为你应该做 一些工作:-)
  • 感谢您的回答。它确实帮助我解决了我的问题。谢谢大家!
【解决方案2】:

借自 Python 的itertools docs

def permutations(iterable, r=None):
    # http://docs.python.org/library/itertools.html#itertools.permutations
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = range(n)
    cycles = range(n, n-r, -1)
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return
for number in permutations('23456789',6): # you said 7, but your examples were 6
    print number

编辑:或者如果你有 Python...

import itertools
for number in itertools.permutations('23456789',6): print number

【讨论】:

    【解决方案3】:

    有 7!=5040 种置换 abcdefg 的方式。

    9C7=36 种方法可以从 123456789 中选择 7 个数字。

    给定一个排列“bfgdeac”和一组{1,3,4,5,6,8,9},有一种自然的方法可以使用排列为该集合提供顺序,即[3, 8,9,5,6,1,4]。

    【讨论】:

    • 这并不能真正回答问题。
    【解决方案4】:

    由于您没有指定语言,因此这里是 Haskell 答案。

    import Control.Monad
    
    selectPerms :: MonadPlus m => [a] -> Int -> m [a]
    selectPerms _        0 = return []
    selectPerms universe n = do
        (digit, remain) <- selectDigit universe
        xs <- selectPerms remain (n - 1)
        return (digit:xs)
    
    selectDigit :: MonadPlus m => [a] -> m (a, [a])
    selectDigit [] = mzero
    selectDigit (x:xs) = capture `mplus` next
        where
            capture = return (x, xs)
            next = do
                (digit, remain) <- selectDigit xs
                return (digit, x:remain)
    
    yourAnswer :: [[Int]]
    yourAnswer = selectPerms [2..9] 7
    

    【讨论】:

    • 比明显的 import Data.List; nub $ map (take 7) (permutations [1..9]) 好多了(但更冗长):) 不过,它仍然读起来很好。
    • @keitamike 你真的应该在某个地方说这些话,比如在标签中 :)
    • 来吧,将 Haskell 翻译成 C 有多难。天哪,我的眼睛在流血! :-)
    • 来吧,真的很简单!我只是选择一个任意排列,然后使用[]MonadPlus 实例将其扩展到所有可能的排列。简单! :)
    猜你喜欢
    • 2012-09-07
    • 2014-06-25
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多