# 查找下一个1稀疏二进制数

27

“下一个”，如“下一个最高”或“具有最小绝对差”？
FUZxxl 2015年

“下一个”与“下一个最高”中的一样。
articuno 2015年

mbomb007

mbomb007'2015-4-2

@articuno我们可以创建一个函数，还是必须是一个完整的程序？功能是非常标准的。
mbomb007'4

13

# Pyth，9个字节

``````f!.&TyThQ
``````

``````               implicit: Q = input()
f      hQ      find the first integer T >= Q + 1,
that satisfies the condition:
!.&TyT        T & (T * 2) is 0
``````

9

# CJam，14 11个字节

DigitalTrauma节省了3个字节。

``````l~{)___+&}g
``````

## 说明

``````l~          "Read and eval input.";
{      }g "Do while...";
)_       "Increment and duplicate (call this x).";
__+    "Get two more copies and add them to get x and 2x on the stack.";
&   "Take their bitwise AND. This is non-zero is as long as x's base-2
representation contains '11'.";
``````

8

# Python 2，44字节

``````n=input()+1
while'11'in bin(n):n+=1
print n
``````

``````\$ echo 12 | python soln.py
16
\$ echo 18 | python soln.py
20
``````

6

# Pyth，12 11字节

``````f!}`11.BThQ
``````

``````               implicit: Q = input()
f        hQ    find the first integer T >= Q + 1,
that satisfies the condition:
!}`11.BT         "11" is not in the binary representation of T
``````

1

orlp 2015年

@orlp谢谢，应该注意到这一点。
2015年

5

# Mathematica，41个 30字节

``````#+1//.i_/;BitAnd[i,2i]>0:>i+1&
``````

3

mbomb007'2015-4-2

4

# Perl，31岁

``````#!perl -p
sprintf("%b",++\$_)=~/11/&&redo
``````

`````` perl -pe'sprintf("%b",++\$_)=~/11/&&redo' <<<"18"
``````

4

# APL，18个字节

``````1∘+⍣{~∨/2∧/⍺⊤⍨⍺⍴2}
``````

``````   1∘+⍣{~∨/2∧/⍺⊤⍨⍺⍴2} 12
16
``````

## 说明

``````1∘+                    ⍝ Increment the input ⍺
⍣{            }     ⍝ until
~∨/               ⍝ none of
2∧/            ⍝ the adjacent coordinates contain 1 1 in
⍺⊤⍨⍺⍴2      ⍝ the length-⍺ binary representation of ⍺.
``````

4

## J，20个字符

``````(+1 1+./@E.#:)^:_@>:
``````

### 说明

``````(+ 1 1 +./@E. #:)^:_@>:
[: (] + [: +./ 1 1 E. #:)^:_ >:
``````

``````    ]                             The argument
+                           plus
[: +./                    the or-reduction of
1 1 E.             the 1 1 interval membership in
#:          the base-2 representation of the argument,
[: (                    )^:_      that to the power limit of
>:   the incremented argument
``````

Zgarb 2015年

@randomra啊，我明白了。
FUZxxl

4

# Javascript，25 19

``````f=x=>x++&2*x?f(x):x
``````

3

# JavaScript（ES6），39 43

``R=(n,x=3)=>x%4>2?R(++n,n):x?R(n,x>>1):n``

``F=n=>{for(x=3;x%4>2?x=++n:x>>=1;);return n}``

``````F = n=>{
do {
++n; // next number
for(x = n; x != 0; x >>= 1) {
// loop to find 11 in any position
if ((x & 3) == 3) { // least 2 bits == 11
break;
}
}
} while (x != 0) // if 11 was found,early exit from inner loop and x != 0
return n
}``````

`%4>2`看起来像是数论的法术，请您解释一下|| 提供链接？

@Jacob（x％4> 2）只是（（x＆3）== 3），但运算符优先级是JS，您可以避免使用2个括号
edc65

3

# Python 2，37个字节

``````f=input()+1
while f&2*f:f+=1
print f
``````

ETHproductions's

ETHproductions'Mar 31''31

2

# JavaScript，75 66 62字节

``function n(a){for(a++;/11/.test(a.toString(2));a++);return a;}``

``````function nextOneSparseNumber(num) {
for (num++; /11/.test(num.toString(2)); num++);
return num;
}``````

2

# 朱莉娅，40个字节

``n->(while contains(bin(n+=1),"11")end;n)``

``````function f(n)

# While the string representation of n+1 in binary contains "11",
# increment n. Once it doesn't, we've got the answer!

while contains(bin(n += 1), "11")
end

return(n)
end``````

``````julia> f(12)
16

julia> f(16)
20``````

2

# > <>（Fish），31 + 3 = 34字节

``````1+:>:  4%:3(?v~~
;n~^?-1:,2-%2<
``````

``````>python fish.py onesparse.fish -v 12
16
``````

`-v`标志添加了3个字节。

1

# JavaScript（ECMAScript 6），40

``````g=x=>/11/.test((++x).toString(2))?g(x):x
``````

# JavaScript，56

``````function f(x){return/11/.test((++x).toString(2))?f(x):x}
``````

1

# Scala，65个字节

``````(n:Int)=>{var m=n+1;while(m.toBinaryString.contains("11"))m+=1;m}
``````

（如果需要命名函数，则解决方案将为69个字节）

1

# Python，39岁 33字节

``f=lambda x:1+x&x/2and f(x+1)or-~x``

``def f(x):x+=1;return x*(x&x*2<1)or f(x)``

xnor

mbomb007'4

0

## Ruby，44岁

``````->(i){loop{i+=1;break if i.to_s(2)!~/11/};i}
``````

@ mbomb007完成。谢谢你的提示。

0

# Matlab（ 77 74字节）

``````m=input('');for N=m+1:2*m
if ~any(regexp(dec2bin(N),'11'))
break
end
end
N
``````

• 测试数字`m+1``2*m``m`是输入。
• `~any(x)``true`，如果`x`包含所有零或者如果`x`是空的

0

C（32字节）

``````f(int x){return 2*++x&x?f(x):x;}
``````

0

# Perl，16个字节

`x&2*x`来自各种答案的答案（我认为是尼克的第一个答案）与nutki的 `redo`收益结合起来：

``````perl -pe'++\$_&2*\$_&&redo'
``````

0

# 果冻，7 个字节

``````‘&Ḥ¬Ɗ1#
``````

### 怎么样？

``````‘&Ḥ¬Ɗ1# - Main Link: n   e.g. 12
‘       - increment           13
1# - 1-find (start with that as v and increment until 1 match is found) using:
Ɗ   -   last three links as a dyad:
Ḥ     -   double v
&      -   (v) bit-wise AND (with that)
¬    -   logical NOT (0->1 else 1)
- implicit print (a single item list prints as just the item would)
``````

0

# Stax，5 个字节

``````╦>ù╤x
``````

• 递增并复制两次。
• 将堆栈顶部减半。
• 按位和堆栈中的前两个元素。
• 如果结果为真（非零），则重复整个程序。
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.