[算法] 找出伪币

【题目】   给你一个装有1 6个硬币的袋子。1 6个硬币中有一个是伪造的,并且那个伪造的硬币比真的硬币要轻一些。你的任务是找出这个伪造的硬币。为了帮助你完成这一任务,将提供一台可用来比较两组硬币重量的仪器,利用这台仪器,可以知道两组硬币的重量是否相同。比较硬币1与硬币2的重量。假如硬币1比硬币2轻,则硬币1是伪造的;假如硬币2比硬币1轻,则硬币2是伪造的。

【算法】

class Program
    {
        static void Main(string[] args)
        {
            List<coin> list = new List<coin>(); // 定义硬币集合
            coin lcoin = new coin(1,1);         // 定义一个轻硬币
            list.Add(lcoin);
            for (int i = 0; i < 15; i++)
            {
                coin hcoin = new coin(i + 2, 2);
                list.Add(hcoin);
            }
            coin newCoin = new coin();
            GetCoin(list,ref newCoin);     //获取最轻的硬币
            Console.WriteLine("Id of the light coin is {0},Its height is {1}", newCoin.Id,newCoin.Height);
            Console.Read();
        }
        /// <summary>
        /// 采用递归方式
        /// </summary>
        /// <param name="list">硬币集合</param>
        /// <param name="refcoin">传递参数 用于返回目标硬币</param>
        public static void GetCoin(List<coin> list,ref coin refcoin)
        {
           
            if (list.Count > 1)
            {
                //硬币分堆
                List<coin> one = new List<coin>();
                List<coin> two = new List<coin>();
                //定义硬币堆重量
                int oneHeight = 0;
                int twoHeight = 0;
                int mid = (int)(list.Count)/2;
                for (int i = 0; i < mid; i++)
                {
                    one.Add((coin)list[i]);
                    oneHeight += ((coin)list[i]).Height;
                  
                }
                for (int j = mid; j < list.Count; j++)
                {
                    two.Add((coin)list[j]);
                    twoHeight += ((coin)list[j]).Height;
                }
                if (oneHeight >= twoHeight)
                {
                    if (two.Count == 1)
                    {
                        refcoin = (coin)two[0];
                    }
                    else
                    {
                        GetCoin(two, ref refcoin);
                    }
                }
                else
                {
                    if (one.Count == 1)
                    {
                        refcoin = (coin)one[0];
                    }
                    else
                    {
                        GetCoin(one ,ref refcoin);
                    }
                }
            }
         
          
        }
    }
    /// <summary>
    /// 定义硬币类,硬币属性:ID、Height
    /// </summary>
   public  class coin
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private int height;

        public int Height
        {
            get { return height; }
            set { height = value; }
        }
        public  coin() { }
        public  coin(int id,int height)
        {
            this.Id = id;
            this.Height = height;
        }

    }

如果硬币的数量为奇数n,则可以分为(n-1)和1个。按照上述方法从(n-1)里找出目标硬币再和这一个单独硬币比较。。

posted @ 2011-07-19 17:18  C公子  阅读(538)  评论(0)    收藏  举报