- プログラミングのお題スレ Part17
581 :デフォルトの名無しさん[sage]:2020/05/23(土) 02:20:43.28 ID:zNKp3hWU - >>579 Ruby
def moves( str ) x, y = 0, 0 r = 0 ta = [ [0,-1], [1,0], [0,1], [-1,0] ] ts = %w{北 東 南 西} str.split(/,\s*/).each{|ch| case ch when '1'; r = (r - 1) % 4 when '2'; r = (r + 1) % 4 when '3' x += ta[r][0] y += ta[r][1] end puts "%3d %3d %s" % [ x, y, ts[r] ] } end moves( "1, 3, 3, 2, 3" ) moves( "3, 2, 3, 1" )
| - プログラミングのお題スレ Part17
582 :デフォルトの名無しさん[sage]:2020/05/23(土) 03:35:28.55 ID:zNKp3hWU - >>579 Ruby
def moves( str ) x, y = 0, 0 dx, dy = 0, -1 ts2 = %w{北 西 \ 東 南 } str.split(/,\s*/).each{|ch| case ch when '1'; dx, dy = dy, -dx when '2'; dx, dy = -dy, dx when '3' x += dx y += dy end puts "%3d %3d %s" % [ x, y, ts2[dx + 2*dy + 2] ] } end moves( "1, 3, 3, 2, 3" ) moves( "3, 2, 3, 1" )
| - プログラミングのお題スレ Part17
583 :デフォルトの名無しさん[]:2020/05/23(土) 13:53:09.08 ID:zNKp3hWU - >>579 Ruby
Vectorで書き直して歩数を追加 require 'matrix' def moves( str ) xy = Vector[ 0, 0, 0 ] v = Vector[ 0, -1, 1 ] ts = %w{ 北 西 x 東 南 } str.split(/,\s*/).each{|ch| case ch when '1'; v = Vector[ v[1],-v[0], 1] when '2'; v = Vector[-v[1], v[0], 1] when '3'; xy += v end puts "%3d %3d %3d歩 %s" % [ xy.to_a, ts[v.dot(Vector[1,2,0])+2] ].flatten } end
|
|