- 今回楽過ぎでは…?
- ただ、
x&y>0
より先にx|y!=x+y
が出てきてしまったのがつらい。- ※ベン図を描くとわかりますが、
(x|y)==(x&y)|(x^y)
です。
- ※ベン図を描くとわかりますが、
Ruby
tyama_henae15.rb
#!/usr/bin/env ruby
#http://nabetani.sakura.ne.jp/hena/orde15nohil/
#http://qiita.com/Nabetani/items/705fa83cfbf20377b92f
def num2place(n)
x=y=0
z=1
while n>0
if n%3==1
x+=z
elsif n%3==2
y+=z
end
n/=3
z*=2
end
[x,y]
end
def place2num(x,y)
return nil if x<0 || y<0 || x&y>0 # x|y!=x+y
n=0
z=1
while x>0||y>0
if x%2>0
n+=z
elsif y%2>0
n+=2*z
end
x/=2
y/=2
z*=3
end
n
end
while gets
x,y=num2place($_.to_i)
puts [[0,-1],[-1,0],[1,0],[0,1]].map{|dx,dy| # this order is important
place2num(x+dx,y+dy)
}.compact*','
STDOUT.flush
end
Python
tyama_henae15.py
#!/usr/bin/env python
#http://nabetani.sakura.ne.jp/hena/orde15nohil/
#http://qiita.com/Nabetani/items/705fa83cfbf20377b92f
import sys
if sys.version_info[0]>=3: raw_input=input
def num2place(n):
x=y=0
z=1
while n>0:
if n%3==1:
x+=z
elif n%3==2:
y+=z
n//=3
z*=2
return (x,y)
def place2num(x,y):
if x<0 or y<0 or x&y>0: return None
n=0
z=1
while x>0 or y>0:
if x%2>0:
n+=z
elif y%2>0:
n+=2*z
x//=2
y//=2
z*=3
return n
try:
while True:
x,y=num2place(int(raw_input()))
a=[place2num(x+dx,y+dy) for (dx,dy) in [(0,-1),(-1,0),(1,0),(0,1)]]
print(','.join(str(e) for e in a if e is not None))
sys.stdout.flush()
except EOFError:
pass
C
- C99準拠のはず
tyama_henae15.c
//http://qiita.com/Nabetani/items/705fa83cfbf20377b92f
//http://nabetani.sakura.ne.jp/hena/orde15nohil/
#include <stdio.h>
#include <stdbool.h>
typedef struct{
int x;
int y;
}dir;
const dir D[]={
{0,-1},{-1,0},{1,0},{0,1} // this order is important
};
void num2place(long long n, long long *px, long long *py){
long long x=0,y=0;
long long z=1;
for(;n>0;){
if(n%3==1){
x+=z;
}else if(n%3==2){
y+=z;
}
n/=3;
z*=2;
}
*px=x;
*py=y;
}
long long place2num(long long x,long long y){
if(x<0 || y<0 || (x&y)>0)return -1;
long long n=0;
long long z=1;
for(;x>0||y>0;){
if(x%2>0){
n+=z;
}else if(y%2>0){
n+=2*z;
}
x/=2;
y/=2;
z*=3;
}
return n;
}
int main(){
long long n,x,y;
for(;~scanf("%lld",&n);){
num2place(n,&x,&y);
bool first=true;
for(int i=0;i<4;i++){
long long m=place2num(x+D[i].x,y+D[i].y);
if(m>=0){
if(!first)printf(",");
first=false;
printf("%lld",m);
}
}
puts("");
fflush(stdout);
}
}