【C#】蜗牛爬井问题C#控制台实现

文章目录


一、问题描述

井深30米,蜗牛在井底,每天爬3米又滑下1米,问第几天爬出来

二、C#控制台代码

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {

            // 以*横向模拟爬的过程
            ClimbDisplay(30, 3, 1);

            // 参数分别为:井高度,向上爬的高度,向下掉的高度
            ClimbDay(30, 3, 1);

            Console.ReadKey();
        }

        /// <summary>
        /// 计算蜗牛爬出井的天数
        /// </summary>
        /// <param name="height">井的高度</param>
        /// <param name="up">向上爬的高度</param>
        /// <param name="down">向下掉的高度</param>
        /// <returns></returns>
        public static void ClimbDay(int height, int up, int down)
        {
            int day = 0;
            int upCount = 0;
            int downCount = 0;

            int count = 0; // 蜗牛实际米数
            while (true)
            {
                // 向上爬
                count = count + up;
                upCount = upCount + up;

                // 下掉以后计算天数
                day++;
                // 向上爬后计算蜗牛高度是否超过井的高度
                if (count > height)
                {
                    break;
                }
                // 向下掉
                count = count - down;
                downCount = downCount + down;
                
            }

            Console.WriteLine("蜗牛第" + day.ToString() + "天就爬出了这口井");
            Console.WriteLine("蜗牛向上爬的总米数:"+ upCount);
            Console.WriteLine("蜗牛向下掉的总米数:" + downCount);
        }

        public static void ClimbDisplay(int height, int up, int down)
        {
            // 定一个List放爬行轨迹数据
            List<int> listClimbPoint = new List<int>();

            int count = 0; // 蜗牛实际米数
            while (true)
            {
                // 向上爬
                count = count + up;

                // 添加向上爬点数
                listClimbPoint.Add(up);

                // 向上爬后计算蜗牛高度是否超过井的高度
                if (count > height)
                {
                    break;
                }
                // 向下掉
                count = count - down;

                // 添加向下掉点数
                listClimbPoint.Add(-down);
            }

            string climbPrint = ""; //用来打印的字符串

            for (int i = 0; i < listClimbPoint.Count; i++)
            {
                if (listClimbPoint[i] > 0)
                {
                    for (int j = 0; j < listClimbPoint[i]; j++)
                    {
                        climbPrint = climbPrint + "*" + " ";

                        Console.WriteLine(climbPrint);

                        if (climbPrint.Length > height * 2)
                        {
                            break;
                        }

                        Thread.Sleep(300);

                        Console.Clear();
                    }
                }
                else if (listClimbPoint[i] < 0)
                {
                    for (int j = 0; j < -listClimbPoint[i]; j++) // 取相反数
                    {
                        climbPrint = climbPrint.Substring(0, climbPrint.Length - 2); ;

                        Console.WriteLine(climbPrint);

                        if (climbPrint.Length > height * 2)
                        {
                            break;
                        }

                        Thread.Sleep(300);

                        Console.Clear();
                    }
                }
            }

            Console.WriteLine("蜗牛已经爬出");
        }
    }
}
相关推荐
小珑也要变强40 分钟前
队列基础概念
c语言·开发语言·数据结构·物联网
吃饭只吃七分饱3 小时前
arm开发板通信
arm开发·c#
AI原吾3 小时前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
机器视觉知识推荐、就业指导3 小时前
Qt/C++事件过滤器与控件响应重写的使用、场景的不同
开发语言·数据库·c++·qt
毕设木哥3 小时前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
珞瑜·3 小时前
Matlab R2024B软件安装教程
开发语言·matlab
weixin_455446173 小时前
Python学习的主要知识框架
开发语言·python·学习
孤寂大仙v3 小时前
【C++】STL----list常见用法
开发语言·c++·list
她似晚风般温柔7894 小时前
Uniapp + Vue3 + Vite +Uview + Pinia 分商家实现购物车功能(最新附源码保姆级)
开发语言·javascript·uni-app
咩咩大主教4 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用