c#流程控制簡易案例

1. 一球從h米高度自由落下,每次落地後反跳回原高度的一半;再落下,求它在 第n次落地時,共經過多少米?第n次反彈多高?
2.3個可樂瓶可以換一瓶可樂,現在有364瓶可樂。問一共可以喝多少瓶可樂,剩下幾個空瓶!
3.猴子每天吃總數一半多一個桃子,第n天只剩下一個桃子。用戶輸入天數n,開始時桃子的總數
4.用一百元買一百隻雞,公雞5元一隻,母雞3元一隻,小雞1元三隻保。證總共花了100元,遍歷所有能買雞的情況,求買雞數爲100的情況

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 流程控制
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        

        private void Form3_Load(object sender, EventArgs e)
        {

        }
        //定義爲初始值爲100,那麼掉落的距離爲100,反彈的高度爲50,
        float h = 100;//初始高度爲100
        int i = 0;//彈跳的次數
        float sum = 0;//經過的距離
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                sum += h + h / 2;
                h /= 2;

            }
            Console.WriteLine("經過了" + sum + "米,第10次反彈" + h + "米");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int kl = 364;//剛開始的可樂數量
            int cs = 0;//喝的次數
            while (kl >= 3)//一但喝的有三瓶 就加一瓶
            {
                kl -= 2;//相當於值喝了兩瓶
                cs += 3;//記錄喝的總數
            }

            cs = cs + kl;//一共喝的數量要加上三瓶換的一瓶

            Console.WriteLine("一共可以喝"+cs+"瓶可樂"+","+"剩下"+kl+"個空瓶!");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int n = 0;        
            n = Convert.ToInt32(textBox1.Text);
            int count = 1;//剩餘的桃子
            for (int i = 1; i <= n; i++)
            {
                count = (count + 1) * 2;//這就是吃之前的數
            }
            Console.WriteLine(count);
        }

        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");
                    }
                }
            }
        }
    }
}

 

發佈了29 篇原創文章 · 獲贊 35 · 訪問量 540
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章