challenge ビンゴの結果を整形表示

重複無し乱数」の続編です。

「重複無し乱数」で作ったbingo関数の結果を下のように「何番目の乱数か」とセットにして10個ずつ折り返して表示するコードを書いてください。

>>> bingo(30)
  1  2  3  4  5  6  7  8  9 10
 29 14 16 13 30 15 22 11 25  9

 11 12 13 14 15 16 17 18 19 20
 23  4 18  5 28 17  8 12 21 20

 21 22 23 24 25 26 27 28 29 30
 26  6  2 19  1  7 10 27  3 24

>>> bingo(35)
  1  2  3  4  5  6  7  8  9 10
  7 15  3 32  1 16 17 28  6 29

 11 12 13 14 15 16 17 18 19 20
 19 23 30 26 20  5 12  2 25 31

 21 22 23 24 25 26 27 28 29 30
 35 13 24 18 11  8 10 34 22 21

 31 32 33 34 35
  9  4 27 33 14

Posted feedbacks - VB.net

bingo関数自体が結果を出力していたので出力部分を変更。
リストに何番目かの値もInsertして出力してます。全然短くできず。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Public Sub bingo(ByVal n As Integer)
    Dim list As New List(Of Integer)
    For i As Integer = 1 To n
        list.Add(i)
    Next
    Dim r As New Random
    For i As Integer = 1 To n * 2
        Dim index As Integer = r.Next(0, n)
        list.Add(list(index))
        list.RemoveAt(index)
    Next

    For i As Integer = 0 To n - 1
        list.Insert((i \ 10) * 20 + i Mod 10, i + 1)
    Next

    For i As Integer = 1 To list.Count - 1
        Console.Write(list(i - 1).ToString.PadLeft(n.ToString.Length) & Space(-CInt(CBool(i Mod 10 <> 0))))
        If i Mod 10 = 0 OrElse i = list.Count - n Mod 10 Then
            Console.WriteLine(New String(ControlChars.Lf, -CInt(CBool(i Mod 20 = 0))))
        End If
    Next
    Console.WriteLine(list(list.Count - 1))
End Sub

Index

Feed

Other

Link

Pathtraq

loading...