[抄道题] 4瓶盖换一啤酒,两空瓶换一啤酒,则已有n啤酒的情况下,最多能喝到多少瓶啤酒?
四个瓶盖能换一瓶啤酒,两个酒瓶也能换一瓶啤酒。现在已经买了n瓶啤酒,那么共能喝到多少瓶啤酒?
(不允许 2个瓶盖+1个酒瓶 换酒等情况)
2015-12-18 16:29

程序代码:#include<iostream>
using namespace std;
void main()
{
int n,i,j,s;
cin>>n;
for(s=0,i=0,j=0;n||i>=4||j>=2;n--)
{
if(i==4)
{
i=0;
n++;
}
if(j==2)
{
j=0;
n++;
}
s++;
i++;
j++;
}
cout<<"最多可喝"<<s<<",还剩瓶盖"<<i<<",剩瓶子"<<j<<endl;
}

2015-12-18 16:52
程序代码:#include<iostream>
using namespace std;
/*
准赊账的前提下:
设一瓶酒中,瓶子价值为x,盖子价值为y,水价值为z
==>2x=x+y+z
4y=x+y+z
==>x=2z,意味着空瓶的价值为水的2倍
y=z,意味着盖子的价值与水相同
则一瓶酒的价值最终可以低4份水。
所以n瓶酒可以换4n瓶酒,且最终没有盖子或瓶子的剩余。
*/
int ZhunShe(int n)
{
return 4 * n;
}
/*
不准赊账时:思路是用递归的方法
n瓶酒相应会产生n瓶n盖,然后以盖和瓶换洒(相应减掉瓶子和盖子数)
换得酒的同时得到盖和瓶(加上相应的瓶子和盖),直到瓶子的个数小于2
且盖子的个数小于4;
*/
int BuShe(int m, int n, int k);
int TOTAL(int N)
{
return BuShe(N, N, N);
}
int BuShe(int m,int n,int k)
{
int total=m,bottle=n, top=k;
int tmp_total = 0;
if(bottle>1||top>3)
{
int tmp_total = bottle / 2 + top / 4; //换得的酒
int tmp_bottle = bottle % 2; //剩余的瓶子
int tmp_top = top % 4; //剩余的盖子
cout << "换酒前共有" << total << "酒," << bottle << "瓶" << top << "盖\n";
cout << "可换" << tmp_total << "酒,剩余" << tmp_bottle << "瓶" << tmp_top << "盖\n";
total += tmp_total;
bottle = tmp_bottle + tmp_total;
top = tmp_top + tmp_total;
cout << "换酒后共有" << total << "酒," << bottle << "瓶" << top << "盖\n\n";
BuShe(total, bottle, top);
}
else
return total;
//return total;一加这句就错了,不加则有一个警告说不是所有的控件路径都返回值,不知道怎么改,请教授一下。
}
int main()
{
int N = 5;
int total=TOTAL(N);
cout << "total:"<<total<<endl;
system("pause");
return 0;
}
程序代码:#include<iostream>
using namespace std;
int ZhunShe(int n); //可以赊账时
int TOTAL(int N);
int BuShe(int m, int n, int k); //不可赊账时
/*---------------函数实现-----------------------*/
int ZhunShe(int n)
{
return 4 * n;
}
int TOTAL(int N)
{
return BuShe(N, N, N);
}
int BuShe(int m,int n,int k)
{
int total=m,bottle=n, top=k;
if(bottle>1||top>3)
{
total = total+(n / 2 + k / 4);
bottle = bottle - (n / 2) * 2 + (n / 2 + k / 4);
top = top - (k / 4) * 4 + (n / 2 + k / 4);
return BuShe(total, bottle, top);//稍稍改下
}
else
return total;
return total;//现在有这句可以了,尽管已经用存在这句了。
}
int main()
{
int N = 5;
int total=TOTAL(N);
cout << "total:"<<total<<endl;
system("pause");
return 0;
}[此贴子已经被作者于2015-12-21 08:37编辑过]
2015-12-19 01:12
程序代码:#include<iostream>
using namespace std;
int beer(int n,int g,int p)
{
if(n<=0&&g<4&&p<2)return 0;
else
{
n=n+(g==4)+(p==2);
if(g==4)g=0;
if(p==2)p=0;
return 1+beer(--n,++g,++p);
}
}
void main()
{
int n;
cin>>n;
cout<<"原始有"<<n<<"瓶酒,最多可喝:"<<beer(n,0,0)<<endl;
}[此贴子已经被作者于2015-12-19 15:12编辑过]

2015-12-19 15:07
2015-12-21 08:35
[此贴子已经被作者于2015-12-21 12:31编辑过]
2015-12-21 08:39
程序代码:#include <stdio.h>
int main()
{
unsigned buy;
printf("买啤酒x瓶:");
scanf("%u",&buy);
for(unsigned drink = 2*buy; drink < 4*buy; drink++) //! 喝酒瓶数处于2倍买酒瓶数和4倍买酒瓶数之间
{
unsigned lib = drink%4;
unsigned bottle = 1;//!瓶一定是剩1个
if(4*buy-2*bottle-lib == drink)
{
printf("喝了%u瓶,剩%u瓶%u盖.\n",drink,bottle,lib);
return 0;
}
[此贴子已经被作者于2015-12-29 21:35编辑过]
2015-12-21 11:26
[此贴子已经被作者于2015-12-21 15:04编辑过]
2015-12-21 14:56
2015-12-21 15:09
看着是数学题最后答案是什么 用程序意义得到什么说下
2022-04-06 15:26