C#組成考題字符串
題目描述
假定已經獲取題庫中的試題號,并存放在數組arrayKT中。例如,?int [] arrayKT={10,13,18,19,20,22,30,31}。定義一個靜態成員方法,該方法實現從上述數組中隨機抽出n(n=arrayKT.Length-1)道考題,并組成一個考題字符串。比如,隨機從arrayKT中抽取n題組成考題字符串:“10,13,18,20,22,30,31”。要求,組成考題字符串中考題不重復,輸出所有可能的字符串。?
輸入
題目的個數
數組中的考題號;
輸出
所有可能的考題字符串;
樣例輸入
5 1 2 3 4 5
樣例輸出
1 2 3 4 1 2 3 5 1 2 4 5 1 3 4 5 2 3 4 5
using System;
using System.Collections;namespace sample
{class Program{static void Main(string[] args){int n;int.TryParse(Console.ReadLine(), out n);ArrayList arr = new ArrayList();string str = Console.ReadLine();String[] s = str.Split(" ".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);int len = s.Length;for (int i = 0; i < len; i++){int temp;int.TryParse(s[i], out temp);arr.Add(temp);}len = arr.Count;ArrayList arr2 = new ArrayList();for (int i = len-1; i >=0; i--){for(int j = 0; j < len; j++){if(j != i){arr2.Add(arr[j]);}}for(int j = 0; j < len-1; j++){if (j != len - 2) { Console.Write("{0} ", arr2[j]); }else { Console.WriteLine(arr2[j]); }}arr2.Clear();}}}
}
?