看板 C_Sharp 關於我們 聯絡資訊
覺得很有趣,就試著寫寫看 http://www.badongo.com/file/20861796 自己跑的時間約是8秒左右 =================================================================== 由於十六進位的數字有20位 16^20 = 1208925819614629174706176 decimal範圍 從 正 79228162514264337593543950335 到 負 79228162514264337593543950335 也就是說,用一個decima可以接受的了20位HEX亂數的範圍 所以我就用三個int組成一個decimal下去做計算 decimal a, b, c; decimal temp; a = Rm亂數.Next(1208925); b = Rm亂數.Next(999999999); c = Rm亂數.Next(999999999); temp = (a * 1000000000000000000M + b * 1000000000M + c); 另外以自己在讀的書上的二元樹範例為樣本,做個不重覆二元樹(簡稱:不元樹) 稍微修改了部分: 加入新數字時,會檢查是否重覆,傳回一個bool值 如果重覆,就不會寫入,並傳回ture 並且,如果產生亂數的主迴圈,收到不元樹傳回ture的話 就把把迴圈的i--重來 所以應該是可以迴避掉重覆的機率 另外,在遇到重覆、超出最大值時都會丟出字串。 但除了故意設定非法樹值去測試之外 我還沒遇到過重覆或是超過最大值的狀況.... 這機率真的很低 以下原始碼: ================================================================== 不過我不會把decimal轉成HEX..... 就沒寫..... 有人能教一下嗎?不元樹.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 不重覆之二元樹 { public class 不元樹<樹節> : IEnumerable<樹節> where 樹節 : IComparable<樹節> { public 樹節 節點資料 { get; set; } public 不元樹<樹節> 大方 { get; set; } public 不元樹<樹節> 小方 { get; set; } private static int 資料數目in = 1; public static int 資料數目 { get { return 資料數目in; } } public 不元樹(樹節 輸入值) { this.節點資料 = 輸入值; this.大方 = null; this.小方 = null; } public bool 插入(樹節 新物件) { 樹節 比對物 = this.節點資料; int 比較大小 = 比對物.CompareTo(新物件); if (比較大小 > 0) { if (this.大方 == null) { this.大方 = new 不元樹<樹節>(新物件); 資料數目in++; return false; } else { return this.大方.插入(新物件); } } else if (比較大小 < 0) { if (this.小方 == null) { this.小方 = new 不元樹<樹節>(新物件); 資料數目in++; return false; } else { return this.小方.插入(新物件); } } else if (比較大小 == 0) { return true; } else throw new Exception("加入檔案時發生不明錯誤"); } #region IEnumerable<TItem> Members IEnumerator<樹節> IEnumerable<樹節>.GetEnumerator() { if (this.大方 != null) { foreach (樹節 物件 in this.大方) { yield return 物件; } } yield return this.節點資料; if (this.小方 != null) { foreach (樹節 物件 in this.小方) { yield return 物件; } } } #endregion #region IEnumerable Members System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } #endregion } } ================================================================== 核心.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using 不重覆之二元樹; using System.IO; namespace 核心執行區 { class 核心 { static void Main(string[] args) { DateTime 起始時間 = DateTime.Now; 生產亂數 亂數 = new 生產亂數(); 亂數.開始(); Console.WriteLine("寫入完成,亂數放在「亂數文件.txt」當中"); string 時間 = (DateTime.Now - 起始時間).ToString(); Console.WriteLine("經歷時間:{0} 秒", 時間); Console.ReadKey(); } class 生產亂數 { Random Rm亂數 = new Random(); public 生產亂數() { } public void 開始() { using (StreamWriter 寫入 = new StreamWriter("亂數文件.txt")) { //宣告接下來會用的變數 decimal a, b, c; decimal temp; //初始化不元樹 Restart: a = Rm亂數.Next(1208925); b = Rm亂數.Next(999999999); c = Rm亂數.Next(999999999); temp = (a * 1000000000000000000M + b * 1000000000M + c); if (temp > 1208925819614629174706176M) goto Restart; 不元樹<decimal> 不元樹倉庫 = new 不元樹<decimal>(temp); 寫入.WriteLine(temp); for (int i = 1; i <= 1000000; i++) { a = Rm亂數.Next(1208925); b = Rm亂數.Next(999999999); c = Rm亂數.Next(999999999); temp = (a * 1000000000000000000M + b * 1000000000M + c); //檢查數字是否合法 if (temp > 1208925819614629174706176M) { Console.WriteLine("數字不合法於:{0}",temp); i--; continue; } //寫入不元樹,如果收到寫入失敗就重來 //(重覆的話會傳會true) if (不元樹倉庫.插入(temp)) { Console.WriteLine("數字重覆於:{0}", temp); i--; continue; } 寫入.WriteLine(temp); } } } } } } ==================================================================== 不過我不會把decimal轉成HEX..... 就沒寫..... 有人能教一下嗎? -- ◢ 鄉民啊!鄉民! 請告訴我誰是最純潔的人! ◢█ │ PTT │ ██ :就是你!Snow White F23ko!│ █◤ ╯ ◤ ﹨(╯▽╰ )∕ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.27.109.99
F23ko:等一下.... FFFFFFFFFFFFFFFFFFFF換算成十進位我算的不太清 02/28 03:58
F23ko:楚..... 最大值還要再確認過.... 02/28 03:59
proach:直覺猜是 -1 02/28 13:31
joefaq:推樓上 02/28 15:46
F23ko:為什麼是-1? 02/28 16:04
liaommx:tree..看到tree就快瘋了XD.. 03/01 10:43
liaommx:目前是用list的方法,list sort,現在在想辦法找有沒有辦法l 03/01 10:44
liaommx:不重複, 03/01 10:44
hpo14:33, 35行的 true 打錯了 03/01 21:22
F23ko:那邊不在程式片段中,錯了沒差 XD 03/01 21:25
virdust2003:你真的用中文當變數名稱? 03/02 07:11
F23ko:真的... 03/02 10:17