/*酒瓶和酒盖换酒
* 四块钱可以买一瓶酒,两个酒瓶换一瓶酒,四个瓶盖换一瓶酒,求可以喝到的酒
* */
# include <iostream>
using namespace std;
class Data{
public:
Data():bottle_(0),cover_(0){}
int Buy(int money){
int tmp;
while(1){
if (money >= 4){
tmp = money/4; //能买到的酒数量
count_ += tmp;
bottle_ += tmp; //每买一瓶酒就有一个酒瓶
cover_ +=tmp; //每买 一瓶酒就有一个瓶盖
money %= 4; //找零
money_ = money; //把找零传递出去
}
if (bottle_ > 1) {
tmp = bottle_/2; //瓶子可以换的酒的数量
bottle_ %= 2; //瓶子换酒后,剩下瓶子的数量
count_ += tmp;
bottle_+= tmp; //每换一瓶酒就有一个酒瓶
cover_ += tmp; //每换一瓶酒就有一个瓶盖
}
if (bottle_ = 1){ //如果仅剩一个酒瓶,酒先虚拟出一个酒瓶
bottle_ = 0; //虚拟酒瓶和剩余的一个酒瓶换一瓶酒之后酒瓶数量为0
count_ += 1;
cover_ += 1; //每换一瓶酒就有一个瓶盖
}
if (cover_ > 3) {
tmp = cover_/4; //盖子可以换的酒的数量
cover_ %= 4; //盖子换酒之后,剩下的盖子的数量
count_ += tmp;
bottle_+= tmp;//每换一瓶酒就有一个酒瓶
cover_ += tmp; //每换一瓶酒就有一个瓶盖
}
else //这里没有考虑虚拟出瓶盖和组合(比如两块钱加一个啤酒瓶)的情况,仅作思路参考
{
break;
}
}
}
void show(){
cout << "找零 : " << money_ << endl;
cout << "酒 : " << count_ << endl;
cout << "酒瓶 : " << bottle_ << endl;
cout << "瓶盖 : " << cover_ << endl;
}
private:
int money_;
int bottle_;
int cover_;
static int count_;
};
int Data::count_ = 0;
int main()
{
Data d;
d.Buy(13);
d.show();
}
* 四块钱可以买一瓶酒,两个酒瓶换一瓶酒,四个瓶盖换一瓶酒,求可以喝到的酒
* */
# include <iostream>
using namespace std;
class Data{
public:
Data():bottle_(0),cover_(0){}
int Buy(int money){
int tmp;
while(1){
if (money >= 4){
tmp = money/4; //能买到的酒数量
count_ += tmp;
bottle_ += tmp; //每买一瓶酒就有一个酒瓶
cover_ +=tmp; //每买 一瓶酒就有一个瓶盖
money %= 4; //找零
money_ = money; //把找零传递出去
}
if (bottle_ > 1) {
tmp = bottle_/2; //瓶子可以换的酒的数量
bottle_ %= 2; //瓶子换酒后,剩下瓶子的数量
count_ += tmp;
bottle_+= tmp; //每换一瓶酒就有一个酒瓶
cover_ += tmp; //每换一瓶酒就有一个瓶盖
}
if (bottle_ = 1){ //如果仅剩一个酒瓶,酒先虚拟出一个酒瓶
bottle_ = 0; //虚拟酒瓶和剩余的一个酒瓶换一瓶酒之后酒瓶数量为0
count_ += 1;
cover_ += 1; //每换一瓶酒就有一个瓶盖
}
if (cover_ > 3) {
tmp = cover_/4; //盖子可以换的酒的数量
cover_ %= 4; //盖子换酒之后,剩下的盖子的数量
count_ += tmp;
bottle_+= tmp;//每换一瓶酒就有一个酒瓶
cover_ += tmp; //每换一瓶酒就有一个瓶盖
}
else //这里没有考虑虚拟出瓶盖和组合(比如两块钱加一个啤酒瓶)的情况,仅作思路参考
{
break;
}
}
}
void show(){
cout << "找零 : " << money_ << endl;
cout << "酒 : " << count_ << endl;
cout << "酒瓶 : " << bottle_ << endl;
cout << "瓶盖 : " << cover_ << endl;
}
private:
int money_;
int bottle_;
int cover_;
static int count_;
};
int Data::count_ = 0;
int main()
{
Data d;
d.Buy(13);
d.show();
}