一道腾讯面试题

 

  
  
  
  
  1. /** 
  2.  * 已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10() 随机1~10。 
  3.  */ 
  4. using System; 
  5. using System.Collections.Generic; 
  6. using System.Linq; 
  7. using System.Text; 
  8.  
  9. namespace ConsoleApplication1 
  10.     class Program 
  11.     { 
  12.         static void Main(string[] args) 
  13.         { 
  14.             Rand10 rand10 = Rand10.GetInstance(); 
  15.             long total = 9999999; 
  16.             //记录1~10的数产生的次数 
  17.             int[] numArray = new int[10]; 
  18.             for (long i = 0; i < total; i++) 
  19.             { 
  20.                 int randomNumber = rand10.Next(); 
  21.                 numArray[randomNumber - 1]++; 
  22.             } 
  23.             //打印产生各数的概率 
  24.             for (int i = 0; i < numArray.Length; i++) 
  25.             { 
  26.                 Console.WriteLine(string.Format("产生{0}的概率是:{1:0.00000}", i + 1, (Double)numArray[i] / total)); 
  27.             } 
  28.             Console.ReadLine(); 
  29.         } 
  30.     } 
  31.     //1~7的随机数产生类 
  32.     public class Rand7 
  33.     { 
  34.         private static Rand7 _rand7; 
  35.         private readonly Random _random = new Random(); 
  36.         private  Rand7() 
  37.         {   
  38.  
  39.         } 
  40.         public static Rand7 GetInstance() 
  41.         { 
  42.             if(_rand7==null
  43.             { 
  44.                 _rand7 = new Rand7(); 
  45.             } 
  46.             return _rand7; 
  47.         } 
  48.         //获得随机数 
  49.         public int Next() 
  50.         { 
  51.             return _random.Next(1, 8); 
  52.         } 
  53.     } 
  54.  
  55.     //1~10的随机数产生类 
  56.     public class Rand10 
  57.     { 
  58.          private static Rand10 rand10; 
  59.          private Rand7 _rand7 = Rand7.GetInstance(); 
  60.         private  Rand10() 
  61.         { 
  62.  
  63.         } 
  64.         public static Rand10 GetInstance() 
  65.         { 
  66.             if(rand10==null
  67.             { 
  68.                 rand10 = new Rand10(); 
  69.             } 
  70.             return rand10; 
  71.         } 
  72.         //获得随机数 
  73.         public int Next() 
  74.         { 
  75.             int num; 
  76.             //均匀产生1、 2 、3、4、5 
  77.             while (true
  78.             { 
  79.                 num = _rand7.Next(); 
  80.                 if (num <= 5) 
  81.                     break
  82.             } 
  83.  
  84.             while (true
  85.             { 
  86.                 int n = _rand7.Next(); 
  87.                 if (n == 1) 
  88.                     continue
  89.                 //n大于4的数字有5、6、7,因为是由Rand7产生的,所以概率均匀 
  90.                 if (n > 4) 
  91.                 //因为num只可取值1、2、3、4、5并且取值概率均匀,num*2可得2、4、6、8、10也概率均匀 
  92.                     num *= 2; 
  93.                 //n小于4的数字有1、2、3,因为是由Rand7产生的,所以概率均匀 
  94.                 else 
  95.                 //因为num只可取值1、2、3、4、5并且取值概率均匀,num*2-1可得1、3、5、7、9也概率均匀 
  96.                     num = num * 2 - 1; 
  97.                 break
  98.             } 
  99.             return num; 
  100.         } 
  101.     } 

 

你可能感兴趣的:(腾讯,面试题,休闲,腾讯面试题,面试题答案)