1000瓶汽水,每喝3瓶赚一瓶,最终喝了多少瓶,剩几个空瓶?
题目:共有1000瓶汽水,每喝完后一瓶得到的一个空瓶子,每3个空瓶子又能换1瓶汽水,喝掉以后又得到一个空瓶子,问总共能喝多少瓶汽水,最后还剩余多少个空瓶子?
[解决办法]
int count = 10;
int empty = 0;
while (count > 0)
{
Console.WriteLine("喝一瓶饮料");
empty++;
count--;
if (empty == 3)
{
Console.WriteLine("3个空瓶子兑换一瓶饮料");
count += 1;
empty = 0;
}
Console.WriteLine("剩余饮料数:{0},剩余空瓶子:{1}", count, empty);
}
Console.Read();
int count = 10;
int flag = 3;
int empty = count / flag + count % flag;
while (empty >= flag)
{
empty = empty / flag + empty % flag;
}
Console.WriteLine("剩余饮料数:0,剩余空瓶子:{0}", empty);
Console.Read();
public int[] DrinkWater(int total)
{
int left = 0;
for (int i = 1; i <= total; i++)
{
left++;
if (left == 3)
{
left = 0;
total++;
}
}
return new int[2] { total, left };
}
function GetGlassnum() {
//饮料瓶
var gn = 1000;
while (gn > 2) {
// 3个瓶换一个饮料
gn = gn - 3;
//
gn = gn + 1;
}
alert(gn);
}
function GetGlassnum() {
var n = 0;
//饮料瓶
var gn = 1000;
while (gn > 2) {
// 3个瓶换一个饮料
gn = gn - 3;
//
gn = gn + 1;
n++;
}
//剩余空瓶数
alert(gn);
//一共喝饮料数
n = 1000 + n;
alert(n);
}
function GetGlassnum() {
//换取的饮料
var n = 0;
//饮料
var gn = 1000;
while (gn > 2) {
// 3个瓶换一个饮料,实际就使少了2个空瓶
gn = gn - 2;
//换取的饮料
n++;
}
//剩余空瓶数
alert(gn);
//一共喝饮料数
n = 1000 + n;
alert(n);
}
//结果 剩余空瓶数:2 共喝饮料数:1499
int count = 1000;
int flag = 3;
int empty = count;
while (empty >= flag)
{
int temp = empty / flag;
count += temp;
empty = temp + empty % flag;
}
Console.WriteLine("共喝饮料数:{0},剩余空瓶子:{1}", count, empty);
Console.Read();
int all = 1000; //总瓶数
int drink = 0; //已经喝掉的数量
int empty = 0; //最后剩余空瓶子
while (all > 0)
{
all--;
drink++;
empty++;
if (empty == 3)
{
empty = 0;
all++;
}
}
Response.Write("已经喝掉的数量:" + drink + " 最后剩余空瓶子:" + empty);
结果:
已经喝掉的数量:1499 最后剩余空瓶子:2
protected void Sum()
{
int full = 1000;
int empty = 0;
int count = 0;
for (int i = 0; ; i++)
{
full--;
empty++;
if (empty == 3)
{
full++;
empty = 0;
}
count = i;
if (full == 0)
{ break; }
}
this.tbCount.Text = count.ToString();
this.tbEmpty.Text = empty.ToString();
}
int totalSoda = 1000;//总共的汽水
int emptyBottle = 0 ;//空瓶子
int count=0;//喝的次数
while(count<totalSoda){
count++;
emptyBottle++;
if(emptyBottle==3){
emptyBottle = 0;
totalSoda++;
}
}
System.out.println("喝的次数:"+count+"\n空瓶子数:"+emptyBottle);
int count = 1000;
int res = count;
int a = 0;
while(count/3 > 0){
res += count/3;
count = count/3 + count%3;
a = count % 3;
}
System.out.println(res);
System.out.println(a);
private static int Test()
{
int a = 1000;
int c = 1;
while (a > 0)
{
a--;
c++;
if (c >= 3)
{
a += 1;
c = 0;
}
}
return c;
}