牛客 牛牛爱喝酒(模拟)
生活随笔
收集整理的這篇文章主要介紹了
牛客 牛牛爱喝酒(模拟)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
鏈接:https://ac.nowcoder.com/acm/contest/9752/A
來源:牛客網(wǎng)
牛牛是一個酒鬼,非常愛喝酒,
一瓶酒m元錢,
兩個酒瓶可以換一瓶酒,
四個瓶蓋可以換一瓶酒,
現(xiàn)在有 n 元錢,求最多可以喝多少瓶酒?
(注:沒有借貸功能,即最終不允許借一瓶酒、喝完后拿酒瓶兌換歸還的操作)
2. 解題
class Solution { public:/*** 代碼中的類名、方法名、參數(shù)名已經(jīng)指定,請勿修改,直接返回方法規(guī)定的值即可* 返回牛牛能喝的最多的酒* @param m int整型 酒單價* @param n int整型 牛牛的現(xiàn)金* @return int整型*/int countWine(int m, int n) {// write code hereif(n < m) return 0;int ans = 0, gai = 0, bottle = 0;while(n/m+gai/4+bottle/2 > 0){int newWine1 = n/m;n -= newWine1*m;ans += newWine1;gai += newWine1;bottle += newWine1;int newWine2 = gai/4;gai += -newWine2*4+newWine2;bottle += newWine2;ans += newWine2;int newWine3 = bottle/2;gai += newWine3;bottle += -newWine3*2+newWine3;ans += newWine3;}return ans;} };我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的牛客 牛牛爱喝酒(模拟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 1669. 合并两个链
- 下一篇: LeetCode 1131. 绝对值表达