10块钱可以喝几瓶?

 

 

/*
 题目:
啤酒2块钱1瓶,
4个瓶盖换1瓶
2个空瓶换1瓶

问:10块钱可以喝几瓶?
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace  Serbia
{
     
    class Beers
    {
        public int Count = 0;
        public int pg=0;
        public int pz=0;
        public int UnitPrice = 2;
        public Beers( )
        { 
        } 
        public Beers(int money)
        {
           Count= money / UnitPrice;
           pg = Count;
           pz = Count;  
        } 

        public Beers getMaxCount()
        {
            while (pg / 4 > 0 || pz / 2 > 0)
            {
                if (pg / 4>0)
                {
                    pg = pg - 4;
                    pg++;
                    pz++; 
                    Count++;
                }
                if (pz / 2>0)
                {
                    pz = pz - 2;
                    pg++;
                    pz++;
                    Count++;
                }
            }
            return this;
        }
    }
}

  

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            _Init();
        }

        private void _Init()
        {
            richTextBox1.Text= @"
            题目:  
            啤酒2块钱1瓶,
            4个瓶盖换1瓶
            2个空瓶换1瓶
            问:10块钱可以喝几瓶?";
            richTextBox1.ReadOnly = true; 
        }
        private Func<int, int, Beers> MCount = (my, credit) =>
        {
            Beers br = new Beers();
            int Count, pg, pz =0;
            Count = my / 2;
            pg=Count;
            pz = Count;
            while (pg / 4 > 0 || pz / 2 > 0)
            {
                if (pg / 4 > 0)
                {
                    pg = pg - 4;
                    pg++;
                    pz++;
                    Count++;
                }
                if (pz / 2 > 0)
                {
                    pz = pz - 2;
                    pg++;
                    pz++;
                    Count++;
                }
            }
            while ((pg+1) / 4 > 0 || (pz+1) / 2 > 0)
            {
                pg++;
                pz++;
                Count++;
                if (pg / 4 > 0)
                {
                    pg = pg - 4;
                    pg++;
                    pz++;
                    Count++;
                }
                if (pz / 2 > 0)
                {
                    pz = pz - 2;
                    pg++;
                    pz++;
                    Count++;
                }
                pg--;
                pz--;
                Count--;
            }
            br.pg = pg;
            br.pz = pz;
            br.Count = Count;
            return br;
        };
        private void button1_Click(object sender, EventArgs e)
        { 
            label2.Text = "";
            Beers be = new Beers(Convert.ToInt16(numericUpDown1.Value));
            //Beers be = MCount(Convert.ToInt16(numericUpDown1.Value),3);
            be = be.getMaxCount();
            int count = be.Count;
            int pz = be.pz;
            int pg = be.pg;

           label2.Text= "一共可以喝" + count + "瓶,剩下" + pz + "瓶子、剩下" + pg + "瓶盖";

        }

  

posted @ 2020-11-10 14:08  后跳  阅读(126)  评论(0编辑  收藏  举报