C# “百钱百鸡 公鸡5元一只,母鸡3元一只,小鸡一元3只,用100元买100只鸡"一种做法

来源:互联网 发布:淘宝客服主管必备技能 编辑:程序博客网 时间:2024/04/29 16:39

 #region 百钱百鸡
            Console.Write("公鸡5元一只,母鸡3元一只,小鸡一元3只,用100元买100只鸡");
            Console.WriteLine("\n");
           
           /*
            int gj=0;
            int mj=0;
            int xj=0;
            int p = 0;
            for (gj = 1; gj <= 19;gj++ )
            {
                for (mj = 1; mj <= 33; mj++)
                {
                    xj = 100 - gj - mj;//获取百中除了公鸡和母鸡后,小鸡的总钱数
                    Math.DivRem(xj, 3, out p);//计算小鸡的个数
                    if (((5*gj+3*mj+xj/3)==100)&&p==0)//如果公鸡、母鸡和小鸡的总钱数加起来为100
                    {
                        Console.WriteLine("公鸡的个数"+gj);
                         Console.WriteLine("母鸡的个数"+mj);
                         Console.WriteLine("小鸡的个数"+xj);
                         Console.WriteLine("\n");
                    }
                }
            }
*/
            int allMoney = 100;//100元
            int allChicken = 100;//100只
            int gj = 5;
            int mj = 3;
            int xj = 1;
            int xjnum = 3;
            for (int x = 0; x <= allChicken; x++)
            {
                for (int y = 0; y <=allChicken; y++)
                {
                    int z = allChicken - x - y;
                    if (z % xjnum != 0) continue;
                    z = z / xjnum;
                    int Money = x * gj + y * mj + z * xj;
                    if (Money==allMoney)
                    {
                        Console.WriteLine("公鸡的个数:{0},母鸡:{1},小鸡:{2}",x,y,z*xjnum);
                    }
                }

            }
            #endregion
            Console.ReadKey();
        }

原创粉丝点击