C_Sharp 板


LINE

覺得很有趣,就試著寫寫看 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
1F:→ F23ko:等一下.... FFFFFFFFFFFFFFFFFFFF換算成十進位我算的不太清 02/28 03:58
2F:→ F23ko:楚..... 最大值還要再確認過.... 02/28 03:59
3F:→ proach:直覺猜是 -1 02/28 13:31
4F:→ joefaq:推樓上 02/28 15:46
5F:→ F23ko:為什麼是-1? 02/28 16:04
6F:推 liaommx:tree..看到tree就快瘋了XD.. 03/01 10:43
7F:→ liaommx:目前是用list的方法,list sort,現在在想辦法找有沒有辦法l 03/01 10:44
8F:→ liaommx:不重複, 03/01 10:44
9F:→ hpo14:33, 35行的 true 打錯了 03/01 21:22
10F:→ F23ko:那邊不在程式片段中,錯了沒差 XD 03/01 21:25
11F:推 virdust2003:你真的用中文當變數名稱? 03/02 07:11
12F:→ F23ko:真的... 03/02 10:17







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Soft_Job站內搜尋

TOP