【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("蜗牛已经爬出");
        }
    }
}
相关推荐
序属秋秋秋1 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
ruan1145142 小时前
MySQL4种隔离级别
java·开发语言·mysql
quant_19863 小时前
R语言如何接入实时行情接口
开发语言·经验分享·笔记·python·websocket·金融·r语言
百锦再7 小时前
详细解析 .NET 依赖注入的三种生命周期模式
java·开发语言·.net·di·注入·模式·依赖
风吹落叶花飘荡7 小时前
2025 Next.js项目提前编译并在服务器
服务器·开发语言·javascript
程序猿多布8 小时前
C# 值拷贝、引用拷贝、浅拷贝、深拷贝
c#
失败又激情的man8 小时前
python之requests库解析
开发语言·爬虫·python
专注VB编程开发20年8 小时前
常见 HTTP 方法的成功状态码200,204,202,201
开发语言·网络协议·tcp/ip·http
有没有没有重复的名字8 小时前
线程安全的单例模式与读者写者问题
java·开发语言·单例模式
阿蒙Amon9 小时前
C#随机数生成全面详解:从基础到高级应用
服务器·网络·c#