遗漏的号码是多少(除数为9)


22

挑战

给定一个整数,该整数可被9整除并有一个缺失的数字,请找到缺失的数字。

缺失的数字可以用数字0-9以外的任何字符表示,只要它是一致的即可。

如果缺少的数字可能是0或9,则输出一些内容来表示。

假设用户足够聪明,只能输入一位数字和一位数字等。

测试用例

在测试用例中,缺失的数字由问号表示

123? -> 3
?999 -> 0 or 9
?0023 -> 4
000?1 -> 8

2
我们可以输出0吗?怎么样[0, 9] (数组或2个数字的列表)?
user202729 '17

1
假设您的意思不是0或9,我想我将将此要求编辑为任何表明它可以是的值。谢谢!
NK1406 '17

3
只是?可能的输入?
xnor

2
是否必须支持前导零?
mbomb007 '17

1
@ NK1406我不建议要求前导零,我可以看到至少在某些语言中需要其他字节来支持它。
暴民埃里克(Erik the Outgolfer)

Answers:



9

爱丽丝,12字节

/o&
\i@/+R9%

在线尝试!

输出0,如果结果可以是0或9。

说明

/   Switch to Ordinal mode.
i   Read all input as a string.
&   Fold the next operation over the characters of this strings.
/   Switch to Cardinal mode (not an operation).
+   Add. Implicitly converts each character to the integer value it represents
    and adds it to the top of the stack. The top of the stack is initially, implicitly
    zero. When the character is "?", it is simply discarded, which adds the top of
    the stack to another implicit zero beneath, so the "?" is effectively skipped.
R   Negate. Multiplies the sum by -1.
9%  Modulo 9. This gives the result.
\   Switch to Ordinal mode.
o   Implicitly convert the result to a string and print it.
@   Terminate the program.

所述&可以被移除,因为基数模式解释原始输入至多2点的整数。
Nitrodon

6

JavaScript(ES6),40个字节

如果可能为0或9,则输出9。

f=_=>9-([..._].reduce((a,b)=>a+~~b,0)%9)

2
欢迎来到PPCG!不错的第一篇文章!
Rɪᴋᴇʀ

2
您无需数数f=; 匿名函数是完全有效的。
毛茸茸的

由于运算符优先级,最外面的括号不是必需的;)
Shieru Asakoto

5

Japt,7个字节

尝试了一些解决方案,但最短的解决方案与大多数其他解决方案相似,只是我们不需要?一开始就替换掉。

可以使用任何非数字字符作为缺少的数字。0解决方案可以是that或时输出9

¬x*J u9

试试吧


说明

字符串的隐式输入U¬拆分为单个字符x的数组,通过忽略任何非数字元素的加法来缩小数组,*J将每个元素乘以-1并u9获得结果的正模数。


5

JavaScript(ES6),18个字节

期望a +作为丢失的数字。返回90或9

s=>9-eval(9+s+9)%9

测试用例



4

Python 2中44 41 35个字节

-6字节归功于RiaD

lambda n:-sum(ord(c)-3for c in n)%9

在线尝试!

用户]缺少数字。
输出0如果丢失的数字可能是0或9。


嗯,在所有测试用例中,我都尝试将其输出0为模棱两可的情况,这是有道理的9%9 == 0
蓬松的

如果你这样做INT(C) - > ORD(C) - 3,如果你选择了错误的字符相应的字符,你并不需要,如果,
利雅得

3

Pyth,9个 7字节

%_s-Qd9

使用空格作为定界符,如果结果可能为0或9,则返回0。

在线尝试

说明

%_s-Qd9
   -Qd   Remove the space from the input.
  s      Convert it to an integer.
%_    9  Negate mod 9.

3

Prolog(SWI),59个字节

0*[].
X*[H|T]:-between(0,9,H),U is mod(X+H,9),U*T.
+X:-0*X.

在线尝试!

是的,逻辑编程!

说明

首先,我们创建一个谓词*,该谓词在应用于零和空列表时成立。当列表的开头在0到9之间并且添加前导数字mod 9时,我们的谓词也会成立。

然后我们定义 +,为0作为第一个参数。也就是说,我们希望数字总和是9的倍数。

Prolog为实际为我们找到解决方案做了所有艰苦的工作。


3

Befunge-93,16个字节

1+!_#@3#.--9%~:#

在线尝试!

James Holderness的Befunge答案的单行版本设法删除了两个字节。这实际上将代码压缩为一行,反转了方向,并利用了Befunge不会在行尾跳过的事实。他建议我发表一个单独的答案并作解释。该代码以*表示丢失的数字,并为0或9输出9。

怎么运行的

1+!_  3  --9% Initialises the pointer going right and initialises the digitsum with 3

1+!_         ~: Gets input and checks whether it is end of input (-1)

      3  - Subtract three from the input. 
           This turns the ASCII values of digits into their mod 9 equivalent ("0"(48)=>45(mod 9)=>0)

          -9% Subtract the value from the digitsum and mod it with 9
              This keeps the digitsum as a negative digit greater than -9
              Repeat this until the input ends

Now this is where it gets tricky.
At the end of input, the stack looks like this:
  -(Digitsum%9), Copy of EOF input (-1)
The pointer is moving left from the _
 +!_ Invert the -1 to get 0 and add it to the digitsum (does nothing but get rid of the -1)
1           %~ Adds 1 mod -1 (no input) = 0 to the stack
         --9 Subtracts 9 from 0, yielding -9 and subtracts that from the digitsum. 
             This makes the digitsum positive and equal to 9-(digitsum%9), our desired value
     @  . Finally, print and exit

* (ASCII值42)被选为缺少字符,因为它抵消了数字和的初始值3。


令您惊讶的是,您仍然可以继续压缩字节。做得好!
James Holderness

2

LaTeX,很多字节(1000 628 614)

\RequirePackage{expl3}
\ExplSyntaxOn
\tl_new:N \l_divnine_input_tl
\int_new:N \l_divnine_sum_int
\def \getnum {
  \typein [ \l_divnine_input_tl ] { Input\space a\space number }
  \tl_map_function:NN \l_divnine_input_tl \divnine_sum:n
  \int_set:Nn \l_tmpa_int { 9 - \int_mod:nn { \l_divnine_sum_int } { 9 } }
  \typeout
    { The\space missing\space digit\space is:\space
      \int_use:N \l_tmpa_int
      \int_compare:nNnT { \l_tmpa_int } = { 0 } { \space or\space 9 }
    }
}
\cs_new:Nn \divnine_sum:n {
  \regex_match:nnT { \d } { #1 } { \int_add:Nn \l_divnine_sum_int { #1 } }
}
\ExplSyntaxOff
\getnum
\stop

LaTeX,难以辨认(348334字节)

\RequirePackage{expl3}\ExplSyntaxOn\int_new:N\1\tl_new:N\2\def\3{\typein[\2]{Input\space a\space number}\tl_map_function:NN\2\4\int_set:Nn\1{9-\int_mod:nn{\1}{9}}\typeout{The\space missing\space digit\space is:\space\int_use:N\1\int_compare:nNnT{\1}={0}{\space or\space9}}}\def\4#1{\regex_match:nnT{\d}{#1}{\int_add:Nn\1{#1}}}\3\stop

LaTeX,132个字节

\RequirePackage{expl3}\typein[\1].\newcount\2{\ExplSyntaxOn\int_gset:Nn\2{9-\int_mod:nn\19}}\typeout{\the\2 \ifnum\2=9or 0\fi}\stop

此代码中只能使用空格作为未知数字。


2

J14 12字节

-2个字节,感谢@BolceBussiere

9|9-[:+/"."0

老实说,我不知道是什么原因"."0解释?为0,但是在我的解释器和TIO上都为0,所以我不会对此提出质疑。(更新:请参阅注释以了解原因)。

这种方法非常简单地获取数字的总和,通过减去9来取反,然后取模9。

在线尝试!


1
在您求和之前,您无需花时间;)
Bolce Bussiere

1
另外,“。不解释'?' 为0,则其解释为空列表。为了使其适合,与填充符,在这种情况下(一个数值数组)为0。Ĵ垫它。
Bolce BUSSIERE

2

果冻11 9 6个字节

|0SN%9

说明

|0     # Read input while removing non-digits (question marks) and removing leading zeros
  S    # Sum the digits
   N   # Negate
    %9 # Mod by 9

输出为0表示结果可能为0或9。

在线尝试!

多亏了Xcoder先生,节省了2个字节。在评估过程中使用“每个快速”()时,将数字分成数字是多余的。

感谢Dennis节省了3个字节。可以将输入与0进行按位或运算,而不用手动将输入解析为数字,同时删除前导零和非数字。


9个字节。欢迎来到果冻高尔夫球场!
Xcoder先生17年

这不能解决答案可能为0或9的情况。此外,的确,欢迎使用Jelly。我也很新,但是到目前为止,它一直很有趣。
dylnan '17

@dylnan解决了这个问题

@Poke哦,没关系,我没有看到要求已更改
dylnan

|0代替fØDV€
丹尼斯


1

Befunge-93(PyFunge)22 21字节

我意识到我不需要用它?来代表缺席的数字,所以我在减去48后使用了9的倍数:x
这让我打高尔夫球了3+,但是由于长度的原因我只节省了1个字节。有条件的第一行:(


我的Befunge-98答案的端口:另外
5个字节以检查我们是否已达到EOF,另外
1个字节按48("0"vs '0),另外
1个字节以打印答案.@
并 1个字节,因为第二个一行
总共有8个字节的空间。

~:0`!#|_"0"-+
 @.%9-<

在线尝试!

产出 0如果丢失的数字可能是0或9。

由于以下原因,这仅在PyFunge解释器中有效。

说明

许多此类解释是从我的Befunge-98解释中复制粘贴的,因为该程序与该程序非常相似。无耻的插头

在程序的第一行中,我们对包括x在内的数字求和,因为它是ASCII值,所以将其视为72。但是,一旦我们将其乘以9,总和将是相同的,因此这无关紧要。

~:0`!#|_"0"-+    THE FIRST LINE
~                Gets a character from input - If it is negative, we've reached EOF
 :0`!            Pushes 0 if the character is positive, 0 otherwise
     #|_         Goes to the next line if the value if 0
                     This also gets the negative value off the stack by using a |
        "0"-     Subtracts 48 to account for taking in ASCII values
            +    Adds this adjusted value to the sum

如果我们仅乘以9,我们将得到错误的数字,因为我们想要9 - (sum % 9)。但是,我们可以做得比更好9\-,它将从9中减去余数:如果在修改之前使总和为负数,则我们将得到正数结果,9 - (sum % 9)在某些解释器中为9。这就是要求我们对Befunge 93和98使用PyFunge解释器的原因,因为它是TIO上唯一做到这一点的工具。其他的给我们一个介于-8和8之间的值,而不是0和8

 @.%9-<    THE SECOND LINE
      <    Redirects the IP onto this line
     -     Subtracts the sum from an implicit 0, making it negative
   %9      Mods the sum by 9
 @.        Prints the digit and exits

好答案!从来没有考虑过使用缺少字符的自定义字符来避免额外的检查。希望您不要介意我在最新答案中偷了这个把戏-我想看看是否可以在参考解释器中使用相同的概念。
James Holderness

1

Befunge-98(PyFunge)15 13字节

我意识到,我并不需要使用?到代表缺席的数字,所以我用一个是9减去48后的倍数:x
这让我的高尔夫球关闭3+

#v~'0-+
q>-9%

在线尝试!

使用 x作为缺少的数字,因为它的ASCII值在减去48后可以被9整除(这很好,因为它通常在数学中用作变量)。

通过退出代码输出(因为q比短1个字节.@
输出0,如果丢失的数位可以是0或9。

由于以下原因,这仅在PyFunge解释器中有效。

说明

在程序的第一行中,我们对数字进行求和,包括x,因为ASCII值而将其视为72。但是,一旦我们将其乘以9,总和将是相同的,因此这无关紧要。

#v~'0-+    THE FIRST LINE
#v~        Pushes the next character from input, and goes to the second line on EOF
   '0-     Subtracts 48 to account for taking in ASCII values
      +    Adds this adjusted value to the sum

如果我们仅乘以9,我们将得到错误的数字,因为我们想要9 - (sum % 9)。但是,我们可以做得比更好9\-,它将从9中减去余数:如果在修改之前使总和为负数,则我们将得到正数结果,与9 - (sum % 9) 某些解释器等效。这就是要求我们对Befunge 93和98使用PyFunge解释器的原因,因为它是TIO上唯一做到这一点的工具。其他的给我们一个介于-8和8之间的值,而不是0和8。

q>-9%    THE SECOND LINE
 >       Redirects the IP onto this line
  -      Subtracts the sum from an implicit 0, making it negative
   9%    Mods the sum by 9
q        Outputs via exit code, ending the program

1

红宝石,22字节

用途'(与0可以被“ 0”整除的都会这样做,包括0其自身,都可以使用)。

输出0表示09

p -(gets.sum+6*~/$/)%9

在线尝试!

说明

p     # print after inspecting
  -(  # unary negative, for reversing result of modulus (a-b%a)
    gets.sum # ascii value of string
    + 6*~/$/ # add six for each size (in chars) of input, so "0" translates to 0
  )%9 # mod 9
      # find remainder after dividing 9
      # except it's inverted so it's remainder to add to divide 9


1

Befunge-93,28 27 19 18个字节

必须归功于Mistah Figgins,其PyFunge答案告诉我,如果您只是确保ASCII值是9的倍数,则不需要特殊检查丢失的数字字符。

还要感谢乔·金Jo King),他表明您不需要将字符完全转换为等效的数字,只需将3减去即可得到相对于基数9的值(ASCII 0减3为45,是9的倍数)。 。

3_v#`0:~--
%9_@.+9

在线尝试!

为此,您应该使用该字符*作为缺少的数字(也可以使用其他字符,但这是最好的)。

输出9如果丢失的数字可能是0或9。

说明

3             Push 3 onto the stack.
 _            Since this is non-zero, drop the 3 and branch left.
3             Now executing right to left, push 3 again.
        --    Negate twice, leaving the value unchanged as our starting sum.

       ~      Now we start the main loop, reading a character from stdin.
    `0:       Duplicate and check if it's > 0, i.e. not end-of-stream.
 _ #          If so, continue to the left.
3        -    Subtract 3 to make the number relative to base 9.
        -     Then subtract it from the total, and repeat the loop.

  v           Once we reach the end of the input, we go down.
  _           Drop the EOF character from the stack and go left.
%9   +9       Mod the total with 9, and add 9.
   @.         Output the result and exit.

本质上,我们是在计算所有数字的总和,外加每数字45(这将在我们对9进行调制时最终被抵消)。从3(我们的起始总数)中减去此总和,然后再用丢失的数字(ASCII)减去39*减3)。同样,3减39是9的倍数,所以当我们用9进行模数修改时,它被抵消了。

所以最后我们要计算所有数字的负和,即mod 9,加上9,即

9 - (digitsum % 9)

这就是我们缺少的数字。


-1字节,如果你减去3而不是48.大多数的它被通过需要得到添加9模抵消
乔金

@JoKing谢谢!永远不会想到这一点。可惜我们现在仍然需要+9,但我看不出有什么方法可以消除它。
James Holderness

通过将其压缩为一行,设法删除了一个字节!我将每个循环的总数更改为%9,然后重用9-在返回的总数中加9。
Jo King

@JoKing我知道它只节省了一个字节,但这真是太好了!值得将其作为新答案发布。如果没有别的,您肯定会得到我的投票。
James Holderness

发表了!设法剃掉了最后一个字节!我认为这是我能做的最后一件事
Jo King


0

PowerShell,40字节

param($a)0..9|?{!(($a-replace'x',$_)%9)}

在线尝试!要么验证所有测试用例

需要输入像'123x'$a。构造一个范围09并使用Where-Object(在此缩写为|?)拉出那些与该子句匹配的整数。该子句接受$a,执行一个正则表达式-replace以用x当前数字替换,$_并使用来获得mod 9 %9。因此,如果9均分,则为零。我们将其取值为布尔值,而不是布尔值,将零变成真,将其他所有东西都变成假,从而满足Where-Object子句。这些结果留在管道上,并且输出是隐式的。



0

视网膜35 34 25字节

如果?可以0 or 9,则结果显示为9

.
$*
1{9}

^
9$*;
+`;1

.

在线尝试

说明

.       Sum of the digits in unary + 1
$*
1{9}    Modulo 9

^       Prepend 9 semicolons, for subtracting
9$*;
+`;1    Subtract from 9

.       Back to decimal

相信\d可以更改为just,.然后将其更改为$*
Kritixi Lithos

嗯对 我?写这篇文章时还没有删除。
mbomb007 '17



@totallyhuman啊,是的。早期版本的另一个残余。
mbomb007 '17


0

Tcl,53个字节

puts [expr 9-([regsub -all (\\d)\\D? 0$argv +\\1])%9]

与其他答案一样,通过不明确地说“ 0或9”来简化此过程。
相反,结果“ 9”表示0或9。

在线尝试!

说明

它的工作原理非常简单。它使用一个正则表达式来:

  • 将参数拆分为单个数字
  • 消除任何非数字问号
  • 用加号使数字交错

然后,它求值9-(sum_of_digits mod 9)以得出1..9中的最终值,然后求出最终值puts

如果0$argv输入的问号在前面,则需要前导0(输入)。转换序列中的前导加号不是问题expr




0

brainfuck,50个字节

+>,[---[<-[<<]>[<+++++++++<]>>-],]-[<+>-----]<---.

在线尝试!

打印0或9的9。缺少的字符由表示:

怎么运行的

Tape Format:
    0 Total Input 0

The total is represented as 9-digitSum%9

+>,[ Start loop with 1 as the total
    --- Subtract 3 from the inputted byte to make the value%9 equal to the digit
    [   While the input byte exists
      <-                 Decrement the total
      [<<]>[<+++++++++<] If the total is 0, reset it to 9
      >>-                Decrement the input byte
    ]
,] Continue loop until there is no input
-[<+>-----]<---. Add 48 to the total to convert the digit to the ascii value and output it

缺少的字符必须是mod 9为4,+ 3的字符,因为我们从正常数字中减去3,而将+1初始化为总数为1。

顺便说一句,为了打高尔夫球,代码中存在很多效率低下的问题,因为每个数字将重置总计5次,而不是如果我减去48而不是3,有时将重置一次。



0

Java 8,36 34字节

s->9-s.map(c->c>57?0:c-48).sum()%9

909都有效时返回。

说明:

在线尝试。

s->                               // Method with IntStream parameter and int return-type
   9-                             //  Return 9, minus:
     s.map(c->c>57?0:c-48).sum()  //   The sum of the digits (ignoring the question mark)
     %9                           //   modulo-9
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.