首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输出N基-10

输出N基-10
EN

Code Golf用户
提问于 2017-01-07 14:59:40
回答 11查看 1.3K关注 0票数 20

挑战:

在您选择的编程语言中,接受整数作为基10中的输入,并在否定表示法中输出它,也称为base -10。

示例算法:

这是取自维基百科的一种算法,用于将VB.NET中的基数10转换为任何负值基。

代码语言:javascript
复制
Function toNegativeBase(Number As Integer , base As Integer) As System.Collections.Generic.List(Of Integer)

    Dim digits As New System.Collections.Generic.List(Of Integer)
    while Number <> 0
        Dim remainder As Integer= Number Mod base
        Number = CInt(Number / base)

        if remainder < 0 then
            remainder += system.math.abs(base)
            Number+=1
        end if

        digits.Insert(0, remainder)
    end while

    return digits
end function

显然,您可以使用任何算法,只要它能够完成挑战。

示例输入/输出:

输入:

代码语言:javascript
复制
12

输出:

代码语言:javascript
复制
192

另一个例子:

输入:

代码语言:javascript
复制
2048

输出:

代码语言:javascript
复制
18168

规则:

您不能使用任何内置方法来解决您的编程语言中存在的问题。

这是一个代码-高尔夫,所以最短的代码获胜!

EN

回答 11

Code Golf用户

发布于 2017-01-07 15:15:41

杰夫特,11字节

代码语言:javascript
复制
_ì ìAn)¥U}a

在线测试!

解释

代码语言:javascript
复制
_ì ìAn)¥U}a  // Implicit: U = input integer, A = 10
_        }a  // Return the smallest non-negative integer Z that returns a truthy value
             // when run through this function:
 ì           //   Convert Z to a list of its base 10 digits.
   ìAn)      //   Interpret this as a list of base -10 digits and convert to a base 10 integer.
       ¥U    //   Return (the result == U).
             // Implicit: output result of last expression
票数 5
EN

Code Golf用户

发布于 2017-01-08 02:28:32

果冻,9字节

代码语言:javascript
复制
Dḅ-10=ð1#

这是负数到整数转换的蛮力逆。

在网上试试!

是如何工作的

代码语言:javascript
复制
Dḅ-10=ð1#  Main link. Argument: n

      ð    Combine the links to the left into a chain and start a new, dyadic
           chain with left and right argument n.
       1#  Repeatedly execute the chain with left argument k = n, n + 1, ... and
           right argument n until the first match is found.
D          Convert k to decimal.
 ḅ-10      Convert the result from base -10 to integer.
     =     Compare the result with n.
票数 5
EN

Code Golf用户

发布于 2017-01-07 15:11:55

批处理,82字节

代码语言:javascript
复制
@set/a"d=%1%%10,n=%1/-10-(a=d>>4),d-=a*10
@if %n% neq 0 %0 %n% %d%%2
@echo %d%%2

Batch的除法被截断为零,所以如果余数是负的,我需要添加1(也添加10到余数)来补偿。然后,这些数字在%2中累积,直到结果变为零。

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

https://codegolf.stackexchange.com/questions/105992

复制
相关文章

相似问题

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