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灯, 水草

请输入看板名称,例如:Boy-Girl站内搜寻

TOP