challenge あみだくじ

次のような書式で与えられた「あみだくじ」があります。
(あみだくじはコード中に埋め込んでも、標準入力や
外部ファイルから読み込んでも、書きやすい方法でかまいません)

A B C D E
| | |-| |
|-| | |-|
| |-| |-|
|-| |-| |
|-| | | |

このあみだくじをたどって
A B C D E
| | |-| |
|-| | |-|
| |-| |-|
|-| |-| |
|-| | | |
B D C A E
のように結果を表示させるプログラムを作ってください。

Posted feedbacks - Groovy

ロジック、ちょっと汚いですね
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def text = """\
A B C D E
| | |-| |
|-| | |-|
| |-| |-|
|-| |-| |
|-| | | |"""

def lines = text.split("\n")

def positions = lines[0].split(/\s+/)

lines[1..-1].each{
    def sepa = it.split(/\|/)[1..-1]
    (0..<(sepa.size())).each{ i ->
        if( sepa[i].trim() == "-" ){
            def temp = positions[i]
            positions[i] = positions[i + 1]
            positions[i + 1] = temp
        }
    }
}

println text
positions.each{ print it.padRight(2) }

Index

Feed

Other

Link

Pathtraq

loading...