C++版本计算n元钱可以喝多少瓶啤酒原理讲解及代码实现

    技术2022-07-12  93

    C++版本计算n元钱可以喝多少瓶啤酒原理讲解及代码实现

    /*! * Copyright (c) 2020,ZYF. * All Rights Reserved. * * \file main.cpp * \brief 计算n元钱可以喝多少瓶啤酒 * * \author ZYF * \date 2020/7/1 22:06:34 * \version 1.0.0 */ #include <iostream> using namespace std; /*! * \brief 计算n元钱可以喝多少瓶啤酒 * \param nBottle : int 瓶子(非空瓶)数量 * \param nBottleCap : int 瓶盖数量 * \param nBottleEmpty : int 瓶子(空瓶)数量 * \returns int : 计算结果 * \throws <exception class> * \remarks * \see */ int GetCount(int nBottle,int nBottleCap,int nBottleEmpty) { /* 每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。 先付钱再喝酒,不能赊账,请用c++实现一个函数计算N元钱可以喝多少瓶啤酒? */ if (nBottleEmpty < 2 && nBottleCap < 4) return nBottle; int nChange_Bottle = nBottleEmpty / 2;//空瓶可以换的瓶酒数量 int nChange_Bottle_Over = nBottleEmpty % 2;//空瓶兑换完瓶酒后剩余的空瓶酒瓶数量 int nChange_BottleCap = nBottleCap / 4;//瓶酒盖能够兑换的瓶酒数量 int nChange_BottleCap_Over = nBottleCap % 4;//瓶酒盖兑换完瓶酒后剩下的瓶酒盖数量 return GetCount(nBottle + nChange_Bottle + nChange_BottleCap, nChange_Bottle + nChange_BottleCap + nChange_BottleCap_Over, nChange_Bottle + nChange_BottleCap + nChange_Bottle_Over); } int main(int argc, char* argv[]) { int nMoeny = 12; int nBootle = nMoeny / 2; int nCount = GetCount(nBootle, nBootle, nBootle); printf("\n%d元,能够喝%d瓶啤酒\n", nMoeny, nCount); return 1; }

    Processed: 0.021, SQL: 9