在您选择的编程语言中,接受整数作为基10中的输入,并在否定表示法中输出它,也称为base -10。
这是取自维基百科的一种算法,用于将VB.NET中的基数10转换为任何负值基。:
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显然,您可以使用任何算法,只要它能够完成挑战。
输入:
12输出:
192输入:
2048输出:
18168您不能使用任何内置方法来解决您的编程语言中存在的问题。
这是一个代码-高尔夫,所以最短的代码获胜!
发布于 2017-01-07 15:15:41
_ì ìAn)¥U}a_ì ì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发布于 2017-01-08 02:28:32
Dḅ-10=ð1#这是负数到整数转换的蛮力逆。
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.发布于 2017-01-07 15:11:55
@set/a"d=%1%%10,n=%1/-10-(a=d>>4),d-=a*10
@if %n% neq 0 %0 %n% %d%%2
@echo %d%%2Batch的除法被截断为零,所以如果余数是负的,我需要添加1(也添加10到余数)来补偿。然后,这些数字在%2中累积,直到结果变为零。
https://codegolf.stackexchange.com/questions/105992
复制相似问题