숫자의 이진 표현이 회문인지 아닌지를 찾기 위해 완전한 프로그램을 작성 하시겠습니까?
Sample Input
5
Sample Output
YES
YES
이진 표현이 회문 또는 NO
기타 인 경우 인쇄 합니다 .
숫자의 이진 표현이 회문인지 아닌지를 찾기 위해 완전한 프로그램을 작성 하시겠습니까?
Sample Input
5
Sample Output
YES
YES
이진 표현이 회문 또는 NO
기타 인 경우 인쇄 합니다 .
답변:
n=bin(input())[2:]
print'YNEOS'[n!=n[::-1]::2]
[n!=n[::-1]::2]
합니까?
$><<%w(YES NO)[(n="%b"%$*)<=>n.reverse]
Michael Kohl의 "% b"% s 트릭 덕분에.
alert((a=(prompt()*1).toString(2))-a.split("").reverse().join("")?"NO":"YES")
추가 정보
prompt()*1
: 문자열을 숫자로 변환하는 빠른 트릭.
.toString(2)
: 자바 스크립트에서 바이너리로 변환하는 방법입니다.
a.split("").reverse().join("")
: There is no native support to reverse string, so you have to convert string to array and array to string.
("[part1]" - "[part 2]")?"YES":"NO"
: -
is a replacement for !=
to save 1 char.
<?=strrev($n=decbin(`cat`))==$n?@YES:@NO;
Test:
php 713.php <<< 5
YES
php 713.php <<< 6
NO
m4
instead of cat
to save one. There's also pg
and dd
(which writes some bytes to stderr).
$_=sprintf'%b',shift;
print reverse==$_?YES:NO
Ruby, 43 characters
puts((n="%b"%gets)==n.reverse ? "YES":"NO")
puts (n="%b"%gets)==n.reverse ? :YES: :NO
‘NO…Ü‘#EbÂQè
:).
bin()
existed
n=bin(input())[2:]
print'YES'if n==n[::-1]else'NO'
['NO','YES'][n==n[::-1]]
No string reverse:
print f(split//,sprintf'%b',shift);
sub f{@_<=1?YES:shift!=pop()?NO:f(@_)}
This one constructs all palindromes up to 2^32.
sub f{
my($x,$l)=@_;
$l+=2,f(($x<<$_)+1+(1<<$l-1),$l)?return 1:0 for 1..15-$l/2;
$x-$ARGV[0]?0:1
}
print f(0,1)+f(0,0)+f(1,1)?YES:NO
13 : ';(]-:|.)#:y{''YES'';''NO'''
0?k=n;n?k=div n 2?(n`mod`2+k*2);f x|x==x?0="YES"|True="No";main=interact$f.read
I wanted to do it without using strings at all.
iterative solution, 78 bytes
for($x=log($n=$argv[1],2);$i<$x&($n>>$i^$n>>$x-$i^1);$i++);echo$i<$x/2?NO:YES;
recursive solution, 113 bytes
function p($n,$x=0){return$n<2?$n:is_pal(($n&(1<<$x=log($n,2)/2)-1)^$n>>$x+!is_int($x));}echo p($argv[1])?YES:NO;
If n
is a binary palindrome, the upper half xor the lower half is also a binary palindrome and vice versa.
a port of the excellent C answer from fR0DDY, 58 bytes
for($x=2*$v=$argv[1];$x/=2;$r=$r*2|$x&1);echo$r-$v?NO:YES;
a binary reverse. Columbus´ egg.
Byte count assumes ISO 8859-1 encoding.
.+
$*
+`(1+)\1
${1}0
01
1
^((.)*?).??((?<-2>.)*$)
$1¶$3
O$^`.(?=.*¶)
^(.*)¶\1
Convert to unary. Convert that to binary. Cut the number in half and remove a middle digit if there is one. Reverse the first half. Match if both halves are equal.
BṚ⁼Bị“YES“NO
Explanation:
BṚ⁼Bị“YES“NO Main link. Arguments: z.
B Binary representation of z.
Ṛ Reversed.
B Binary representation of z.
⁼ Check if x is equal to y.
“YES“NO [['Y', 'E', 'S'], ['N', 'O']]
ị xth element of y (1-indexed).
Before printing, Python's str
function is mapped through a list, and then the elements are concatenated, so you see YES
or NO
.
function paly(n)
v=de2bi(n);
if v==v(numel(v):-1:1)
'YES'
else
'NO'
end
Java, 97 85 characters
return Integer.toBinaryString(i).equals(new StringBuffer(s).reverse()+"")?"YES":"NO";
String s=Integer.toBinaryString(i); return s.equals(new StringBuffer(s).reverse()+"")?"YES":"NO";