面试题之百钱百鸡和小明买汽水

来源:互联网 发布:淘宝上卖高仿名牌包包 编辑:程序博客网 时间:2024/04/29 23:25

公鸡5钱一只,母鸡3钱一只,小鸡3只一钱 100块钱怎样能买一百只鸡

  for (int i = 0; i <= 100; i++)
            {
                for (int j = 0; j <= 100; j++)
                {
                    for (int k = 0; k <= 100; k++)
                    {
                        if (5 * i + 3 * j + k == 100 && i + j + 3 * k == 100)
                        {
                            Console.WriteLine("公鸡数量:{0} 母鸡数量:{1} 小鸡数量{2}", i, j, 3 * k);
                            Console.ReadKey();
                        }
                    }
                }
            }

小明拿22快钱去买汽水喝,一瓶汽水1块钱,三个空瓶就可以在换1瓶汽水,问这22快钱小明最多能喝多少瓶汽水?

  public int money = 22;
        public int cokecount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            
        int a=GetAllCoke(money);
        Response.Write(a.ToString()); 
        }

        public  int GetAllCoke(int money)
        {
            if (money > 0)
            {
                cokecount = cokecount + 1;
                money = money - 1;
                if (cokecount % 3 == 0 && cokecount > 0)
                {
                    money = money + 1;
                }
                return GetAllCoke(money);
            }
            else
            {
                return cokecount;
            }
        }

0 0
原创粉丝点击