链上的数字


15

可以显示一些正整数具有称为链可除性的属性对于要被n整除的数字  ,它必须满足三个要求:

  1. 每个数字除以由 n个  数字数字。

    例如,该数目是7143链被2整除,因为7划分114和分歧43.据连锁被3整除,因为7不划分143。

  2. 考虑到可除性的每个子序列都不能有前导零。

    例如,数字14208不能被2整除,因为08具有前导零。但是,它可以被3整除,因为208没有前导零。

  3. 数字中的所有数字必须唯一。

例如,数字14280可被2、3和4链除。如果我对链可除性的解释不清楚,请在评论中提出问题。

输入值

程序的输入包含一个整数n,后跟一个空格,然后是一个已将某些数字替换为下划线的数字。例如,以下是可能的输入:

3 6__2__4508

n将大于1。该数字永远不会完全下划线。您不能保证第一位不是下划线。第一个数字将永远不会为0。Ñ永远不会大于或等于在号码位数。

输出量

输出数字,并用整数替换数字,以使所得数字可被n整除。如果存在不止一种完成链可分割数的方法,则任何一种都可以用作输出。如果没有数字可以完成,请输出no answer。例如,示例输入的输出可能是:

6132794508

这是代码高尔夫球,所以最短的代码获胜。


我假设,如果n大于或等于该数字中的位数,该数字是否可以被链整除?
John Dvorak 2014年

@Jan Dvorak n永远不会等于或大于输入中的位数。它将始终较小。我将进行编辑以反映这一点。
苦艾酒

我们需要编写一个完整的程序,还是一个函数就足够了?
John Dvorak 2014年

@马丁是的。字符限制填充。
苦艾酒

@Jan Dvorak完整程序。
苦艾酒

Answers:


5

Bash + coreutils,197个字节

for i in $(eval printf '%s\\n' ${2//_/{0..9\}}|grep -vP '(\d).*\1');{
for((f=d=0;d<${#i}-$1;d++));{
((${i:d+1:1}==0||10#${i:d+1:$1}%${i:d:1}))&&f=
}
[ $f ]&&echo $i&&((c++))
}
((c))||echo no answer

输出:

$ ./chain.sh 3 714_
7140
$ ./chain.sh 2 7141
no answer
$ ./chain.sh 2 14208
no answer
$ ./chain.sh 3 14208
14208
$ ./chain.sh 2 1_208
no answer
$ ./chain.sh 3 1_208
14208
$ ./chain.sh 2 6__2__4508
no answer
$ ./chain.sh 3 6__2__4508
6132794508
$

说明

  • 参数扩展${2//_/{0..9\}}用替换所有下划线{0..9}
  • eval编辑所得的字符串以扩展所有这些大括号表达式。
  • grep杂草出那里有任何重复数字的一切可能性。
  • 然后检查条件1和2的每个剩余数字。

2

蟒蛇- 239 267

from itertools import*
T=raw_input()
n=int(T[0])
N=len(T)-2
J=''.join
for i in permutations('0123456789',N):
 if all([S in[I,'_']for S,I in zip(T[2:],i)])*all([i[j]>'0'<i[j+1]and int(J(i[j+1:j+n+1]))%int(i[j])<1for j in range(N-n)]):print J(i);exit()
print'no answer'

慢,但短。只需将每个可能的N位排列与给定模式进行比较,然后检查所有要求。我只用7或8位数字进行了测试。应该也可以使用9或10,但要花相当长的时间。

编辑:我添加了缺少的默认输出“无答案”。


2

数学红宝石,349个 224 229字节

n=$*[0].to_i
r='no answer'
(?0..?9).to_a.permutation($*[1].count'_'){|q|s=$*[1]
q.map{|d|s=s.sub'_',d}
c=s.chars
(t=1
c.each_cons(n+1){|c|e=c.shift.to_i
(t=!t
break)if e<1||c[0]==?0||c.join.to_i%e>0}
(r=s)if t)if c==c.uniq}
$><<r

这是一个非常幼稚的实现。我计算下划线的数量,然后简单地创建该长度的所有数字排列的列表,以蛮力每种可能的组合。对于大量的下划线,这将非常糟糕,但这是代码高尔夫球,而不是最快的代码。:)

编辑:从Mathematica移植了这个。查看原始版本的编辑历史记录。

编辑:修复了主要的下划线情况。


不应该使用置换来代替元组(忽略字符数)吗?
DavidC 2014年

@DavidCarraher为什么?我会在那儿错过很多组合,不是吗?
Martin Ender 2014年

数字中的每个数字必须唯一。 Tuples不强加此约束。 Permutations前提是输入集中没有重复的数字。而且,您只能置换尚未使用的数字。(尽管再次,这可能会延长您的代码。)
DavidC 2014年

@DavidCarraher哦,我忽略了唯一性要求。然后,我需要将其添加到内部循环中,在这种情况下,我最好坚持使用Tuples它,因为它更短。
Martin Ender 2014年

@DavidCarraher固定。
Martin Ender 2014年

1

爪哇,421

class C{static int n;public static void main(String[]a){n=new Short(a[0]);f(a[1]);System.out.print("no answer");}static void f(String s){if(s.contains("_"))for(int i=0;i<=9;i++)f(s.replaceFirst("_",i+""));else{for(int i=1;i<s.length()-n+1;){String t=s.substring(i,i+n);if(t.charAt(0)<49||new Long(t)%new Long(s.substring(i-1,i++))>0||s.chars().distinct().count()<s.length())return;}System.out.print(s);System.exit(0);}}}

少打高尔夫球,并附上解释:

class C {

    static int n;

    public static void main(String[] a) {
        n = new Short(a[0]);
        f(a[1]);
        System.out.print("no answer");
    }

    /**
     * This method is called recursively, each time with
     * another underscore replaced by a digit, for all possible digits.
     * If there is a solution, the method prints it and exits the program.
     * Otherwise, it returns.
     */
    static void f(String s) {
        if (s.contains("_")) {
            for (int i = 0; i <= 9; i++) {
                f(s.replaceFirst("_", i + ""));
            }
        } else {
            for (int i = 1; i < s.length() - n + 1;) {
                String t = s.substring(i, i + n);       // on each substring...
                if (                                    // test for the three rules
                    t.charAt(0) < 49 ||
                    new Long(t) % new Long(s.substring(i - 1, i++)) > 0 ||
                    s.chars().distinct().count() < s.length()
                ) {
                    return;            // a rule was broken
                }
            }
            System.out.print(s);       // if we made it this far, it's a success!
            System.exit(0);
        }
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.