C#用一百元买一百只鸡,公鸡5元一只,母鸡3元一只,小鸡1元三只保。证总共花了100元,遍历所有能买鸡的情况,求买鸡数为100的情况 案例

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace 流程控制
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
 
    private void button4_Click(object sender, EventArgs e)
        {
            int gj = 0;
            int mj = 0;
            int xj = 0;
            for (gj = 0; gj <= 20; gj++)//公鸡的钱数就是他的判断条件  最多十九只公鸡
            {
                for (mj = 0; mj <= 33; mj++)//一只母鸡3元  最多33只
                {
                    xj = 100 - gj - mj;//获取百中除了公鸡和母鸡后 小鸡的总钱数
                 
                    if (5 * gj + 3 * mj + xj / 3 == 100)//如果公鸡 母鸡 小鸡的总钱数加起来为100
                    {
                        Console.WriteLine("公鸡的个数" + gj);
                        Console.WriteLine("母鸡的个数" + mj);
                        Console.WriteLine("小鸡的个数" + xj);
                        Console.WriteLine("\n");
                    }
                }

}

}

}
发布了16 篇原创文章 · 获赞 24 · 访问量 1744

猜你喜欢

转载自blog.csdn.net/Cocksuck/article/details/103308567