クラスでペアプログラミングを行うことになった。
コンソールにカンマ区切りで生徒の名前を入力すると、ランダムにペアの組み合わせを作成するプログラムを作成せよ。
その際、ドライバーにはD:ナビゲータにはN:を付与する。
ただし、生徒数が奇数の場合は先生が加わるもとのとする。
[実行例1]
田中,鈴木,佐藤,山口,島田,山田[enter]
{D:山口,N:佐藤}
{D:鈴木,N:田中}
{D:山田,N:島田}
[実行例1]
田中,鈴木,佐藤,山口,島田,山田,原口[enter]
{D:原口,N:先生}
{D:山田,N:鈴木}
{D:山口,N:島田}
{D:田中,N:佐藤}
[実行例]
02 | using System.Collections.Generic; |
07 | static void Main(string[] args){ |
09 | var list = Console.ReadLine().Split( ',' ).ToList(); |
10 | if (list.Count %2 != 0 ){ |
14 | Random random = new Random(); |
15 | for ( int i = list.Count; i > 1;) |
17 | var index = random.Next(i--); |
18 | string temp = list[index]; |
19 | list[index] = list[i]; |
22 | for ( int i = 0; i <list.Count;i++){ |
23 | Console.WriteLine($ "{{D:{list[i++]},N:{list[i]}}}" ); |
コメント