[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
题目:3个可乐瓶可以换一瓶可乐,现在有364瓶可乐。
问:一共可以喝多少瓶可乐,剩下几个空瓶!
首先先看题目 这是道数学题
(好家伙 真没想到还有数学问题qaq)
现在拿出草稿纸一起写:
3 个可乐瓶可以换 1 瓶可乐,
那么 364 瓶可乐就可以换 364/3 瓶可乐(由于/两边都是整数,所以得到的结果一定也是整数),
剩下 364%3 瓶可乐换不了,因为不满3瓶了嘛~
所以第一波,
喝的总数就是:(喝的+换的)
364 + 364/3
364 +121 =485
剩下的空瓶就有:(换的+没够换剩下的)
364/3 + 364%3
121+ 1 = 122
再来第二波,
喝的总数就是:(第一波喝的+第一波剩下的空瓶/3)
(364 + 364/3)+(364/3 + 364%3)/ 3
485 + 122 / 3 = 525
剩下的空瓶就是:(第一波剩下的空瓶/3 + 第一波剩下的空瓶%3)
(364/3 + 364%3)/3+(364/3 + 364%3 )%3
122 / 3 + 122 % 3 = 42
这时候可以发现,喝的可乐会越来越多,剩下的空瓶会越来越少;
那么什么时候这个循环结束呢,就是空瓶不足以再换新的可乐的时候,也就是空瓶数量 < 3 的时候;
已知有364瓶可乐,那么设置可乐数量:int sum=364;
瓶子的数量:int ping = sum; //瓶子的数量和可乐数量是相同的
这时进行循环,只要瓶子的数量大于等于3(可以兑换新可乐),那么就执行循环;
while (ping >= 3) { sum += ping / 3; ping = ping / 3+ ping % 3; }
然后输出一共可以喝多少瓶可乐,剩下多少空瓶就可以了
源代码如下:
int sum = 364; int ping = sum; while (ping >= 3) { sum += ping / 3; ping =
ping / 3+ ping % 3; } Console.WriteLine("一共可以喝多少瓶可乐:" + sum + "\n还剩多少瓶:" +
ping);
运行结果如下:
这道题目主要是研究数学算法,逻辑还是需要好好学习的呀!
最后给题目添加了一个小功能,可以输入可乐的数量和兑换方式:
Console.WriteLine("现在有几瓶可乐?"); int sum = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("几瓶可乐能整一瓶?"); int value =
Convert.ToInt32(Console.ReadLine()); int ping = sum; while (ping >= value) {
sum += ping / value; ping = ping / value + ping % value; }
Console.WriteLine("一共可以喝多少瓶可乐:" + sum + "\n还剩多少瓶:" + ping);
运行结果是这样子的:
制作不易,来个一键三连叭!!