int num 304; int aN =0,bN=0,cN=0; do { aN = num/3; bN = num%3; cN += aN; num = aN+bN; } while(num>=3); 最后cN = 151,num = 2;剩下两个瓶子
Private Sub Form_Load() a = 304 b = 0 c = 0Do While a > 3 b = b + a a = CInt(a / 3) + a Mod 3 c = a Mod 3 MsgBox a Loop MsgBox b & c End Sub共456瓶 2个空瓶
很笨的方法: public static void main(String[] args) { int total = 0; //最后喝的瓶数 int number = 304; int empty = 0 ; //空瓶数 while(number > 2){ number--; empty++; if (empty ==3){ empty = 0 ; number++; } total++; } if (empty>=1) total = total + 3; else total = total + 2; System.out.println(total); System.out.println(empty); } 结果: 455 2
Private Sub Form_Load() a = 304 b = 0 c = 0Do While a >= 3 c=CInt(a / 3) a = CInt(a / 3) + a Mod 3 b = b + c Loop MsgBox b End Sub
public class CalQul { public static void main(String[] args) { CalQul.drinkBeerAccount(); }
public static void drinkBeerAccount() { int leftBottles=304; // 当前剩的空瓶子总数 int drinkTol=304; // 总共喝的啤酒瓶数 while(leftBottles>=3) { leftBottles=leftBottles-2; drinkTol++; } System.out.println("共喝了"+drinkTol+"瓶酒"); System.out.println("剩"+leftBottles+"个空瓶");
} } 运行结果: 共喝了455瓶酒 剩2个空瓶
public int Drink(int beer, int bottle) { if((beer+bottle) < 3) return beer; return beer + Drink( (beer+bottle)/3, (beer+bottle)%3); } 可以喝455瓶
很有意思!可惜我java刚学写不出代码来,呵呵
具体算法(C#): using System; using System.Collections.Generic; using System.Text;namespace ConsoleApplication1 { class Program { public void sum(int i) { int s=0; // 总和 int n=i; //新酒 int empty=0; //空瓶 int newempty = 0; //新增空瓶 int newnew=0; //新增瓶子 while(n!=0) { s=s+n; newempty= (n+empty)% 3; newnew = (n+empty) / 3; n = newnew; empty = newempty; System.Console.WriteLine("" + s + " " + n + " " + newempty); }
package com.briup.test2;import java.util.Scanner;public class Drunk { public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNextInt()){ int x = scan.nextInt(); int sum = 0; while(x>=3){ sum += x/3; x = x-x/3*3+x/3; } System.out.println("empty bottle:\t"+x +"\ntotal drunk:\t"+sum); } } } 简单的题目,最后剩2个瓶子,喝了151瓶。
import java.util.*;class beer { public static void main(String [] args) { int full=304; //满的啤酒 int empty=0; //空瓶 int drink=0; //喝了多少 while(full>0) { full-=1; empty+=1; if(empty==3) { empty=0; full+=1; } drink+=1;
每3个空瓶,才可以换1瓶新啤酒,
喝完1瓶之后可获得一个空瓶
相当于2个空瓶== 1瓶啤酒(不包含酒瓶)
但,无论在什么情况下,手里都会至少会有1个空瓶。
因此,如果有N瓶啤酒,能喝到的啤酒为
N+(N-1)/2瓶 (取整数)
所有304瓶啤酒能喝到的啤酒数为
304+(304-1)/2=304+151=455瓶。
{
int b=304,e=0,s=0;//b,这次能喝得酒,e,上次剩余得空瓶,s,累计喝多少瓶
while(b>0||e>2)//当还有酒喝或者空瓶大于2个(可以继续喝)
{
s=s+b;//先喝酒(累计喝得瓶数)
e=e+b;//产生空酒瓶
b=e/3;//换酒,下次喝
e=e%3;//还剩空瓶(下次接着换)
System.out .println("到这次共喝了"+s+"瓶,剩余"+e+"空瓶,产生"+b+"瓶新酒。");
}
}
到这次共喝了304瓶,剩余1空瓶,产生101瓶新酒。
到这次共喝了405瓶,剩余0空瓶,产生34瓶新酒。
到这次共喝了439瓶,剩余1空瓶,产生11瓶新酒。
到这次共喝了450瓶,剩余0空瓶,产生4瓶新酒。
到这次共喝了454瓶,剩余1空瓶,产生1瓶新酒。
到这次共喝了455瓶,剩余2空瓶,产生0瓶新酒。
long sum = sum_init; //总共喝了多少瓶
long sum_void = sum_init; //空瓶子
while(sum_void >= 3){
sum += sum_void / 3;
sum_void = ( sum_void / 3) + sum_void%3;
}
System.out.println( sum);
}
================
import java.util.*;
public class TestJoy {
List<Beer> list = new ArrayList<Beer>();
class Beer{
boolean full = true; //是否喝掉
boolean own = true;//是否属于zzw
}
class zzw{
int count = 0;
int count2 =0;
public void dirnk(Beer b){
b.full = false; //喝掉
count ++;
}
public void check(){
if((count%3) == 0){
for (int i = 0; i < list.size(); i++) {
if(list.get(i).full == false){
list.get(i).own = false; //可以换给老板了
}
}
list.add(new Beer()); //三空瓶换一瓶
}
}
}
public static void main(String[] args) {
TestJoy t = new TestJoy();
for (int i = 0; i < 304; i++) {
t.list.add(t.new Beer());
}
zzw z = t.new zzw();
for(int i=0;i<t.list.size();i++){
if(t.list.get(i).full == true )
z.dirnk(t.list.get(i));
z.check();
}
System.out.println("喝掉的瓶数:"+t.list.size());
for(int i = 0;i<t.list.size();i++){
if(t.list.get(i).own == false){
t.list.remove(t.list.get(i));
z.count2++;
}
}
System.out.println("换掉的瓶子数目"+z.count2);
Drink(304);
}
public static void Drink( long sum_init){
long sum = sum_init; //总共喝了多少瓶
long sum_void = sum_init; //空瓶子
while(sum_void >= 3){
sum += sum_void / 3;
sum_void = ( sum_void / 3) + sum_void%3;
}
System.out.println( sum);
}
}
=========
out:
喝掉的瓶数:455
换掉的瓶子数目227
455
int aN =0,bN=0,cN=0; do
{
aN = num/3;
bN = num%3;
cN += aN;
num = aN+bN;
}
while(num>=3);
最后cN = 151,num = 2;剩下两个瓶子
a = 304
b = 0
c = 0Do While a > 3
b = b + a
a = CInt(a / 3) + a Mod 3
c = a Mod 3
MsgBox a
Loop
MsgBox b & c
End Sub共456瓶 2个空瓶
public static void main(String[] args) {
int total = 0; //最后喝的瓶数
int number = 304;
int empty = 0 ; //空瓶数
while(number > 2){
number--;
empty++;
if (empty ==3){
empty = 0 ;
number++;
}
total++;
}
if (empty>=1)
total = total + 3;
else
total = total + 2;
System.out.println(total);
System.out.println(empty);
}
结果:
455
2
a = 304
b = 0
c = 0Do While a >= 3
c=CInt(a / 3)
a = CInt(a / 3) + a Mod 3
b = b + c
Loop
MsgBox b
End Sub
k=0;
while(i>3)
{
k+=i/3;
i=(i%3)+(i/3);//取余数,和除数
} printf("喝了多少瓶:%d 还有多少空瓶%d:\r\n",k,i);
i=304;
k=304;
while(i>3)
{
k+=i/3;
i=(i%3)+(i/3);
} printf("喝了多少瓶:%d 还有多少空瓶%d:\r\n",k,i);
{
int i=304;//³õʼ»¯
int sum=i;//ºÈµÄÆ¡¾ÆÊý
int rest;//Ê£ÓàµÄ¿ÕÆ¿
int count=1;//»»µÄÆ¿Êý
while(count>0)
{
rest=i%3;
count=i/3;
sum=sum+count;
i=rest+count;
}
printf("%d %d",sum,rest);
return 0;
}输出 455 2
{
int i=304;
int sum=i;
int rest;
int count=0;
while(count>0)
{
rest=i%3;
count=i/3;
sum=sum+count;
i=rest+count;
}
printf("%d %d",sum,rest);
return 0;
}输出 455 2
b=a%3;
c=aMODb;
sum=0;if (a>=3)
{
a=b+c;
sum+=a;
}
如果老板借你一瓶,就是456
else 455
int i=0;
int m=0;
for(n=304;i<3;n=i)
{
i=n/3+n%3;
m+=i;
}
喝了的酒共:(m+304)瓶
int i=0;
int m=0;
for(n=304;i>3;n=i)
{
i=n/3+n%3;
m+=i;
}
喝了的酒共:(m+304)
纠正上贴
int i=n/3+n%3;
int m=0;
for(n=304;i>3;n=i)
{
i=n/3+n%3;
m+=i-n%3;
}
喝了的酒共:(m+304)
再纠正
*
*/
package com.test.app;/**
* @author Liusj
*有304瓶啤酒,每3个空瓶子能换一瓶新啤酒,问能喝到多少瓶啤酒
*/
public class Drink { /**
* @param args
*/
public static void main(String[] args) {
int k=304;//手中瓶数
int m=0; //喝酒瓶数
int n=0; //空瓶数
for(k=304;k>=3;){
m++;
n++;
if(m%3==0){
k=k-2;
n=0;
}
}
m=m+k;
n=k;
System.out.println(m);
System.out.println(n);
}
}
每3个空瓶,才可以换1瓶新啤酒,
喝完1瓶之后可获得一个空瓶
相当于2个空瓶== 1瓶啤酒(不包含酒瓶)
但,无论在什么情况下,手里都会至少会有1个空瓶。
因此,如果有N瓶啤酒,能喝到的啤酒为
N+(N-1)/2瓶 (取整数)
所有304瓶啤酒能喝到的啤酒数为
304+(304-1)/2=304+151=455瓶。
无论在什么情况下,手里都会至少会有1个空瓶。 和你的答案有什么关系???
这句话如果你不理解。那就给你解释一下。前提是店主不会借给你空瓶子的,那怎么办,自己借给自己。
形象一点就是,你左手放1瓶啤酒,右手放N-1瓶啤酒
左边喝1瓶,右边喝2瓶,喝完就换,换来的放在左边。
接着喝……
所以你换得次数就是右手边的啤酒总数/2(取整),而你的左手边永远都有瓶子。
然后就得出公式:
总数 = N +(N-1)/2瓶 (取整数)
public class testbe {
static int i=304;
static int k=304;
public static void main(String[] ager){
while(i>3){
k=k+i/3;
i=(i%3)+(i/3);//取余数,和除数
}
System.out.print("喝了多少瓶:"+k+"还有多少空瓶:"+i);}
}
int total = 0; //总共喝的啤酒瓶数
int initBeer = 304 ; //当前拥有的啤酒瓶数
int cover = 0; //瓶盖数
public void method(){
while(initBeer>0){
total++;
initBeer--;
cover++;
if(cover==3){
initBeer++;
cover=0;
}
}
System.out.println("总共喝的啤酒瓶数:"+total);
System.out.println("剩下的瓶盖数:"+cover);
} public static void main(String[] args) {
TestA t=new TestA();
t.method();
}
}运行结果: 总共喝的啤酒瓶数 455
剩下的瓶盖数 2
CalQul.drinkBeerAccount();
}
public static void drinkBeerAccount()
{
int leftBottles=304; // 当前剩的空瓶子总数
int drinkTol=304; // 总共喝的啤酒瓶数
while(leftBottles>=3)
{
leftBottles=leftBottles-2;
drinkTol++;
}
System.out.println("共喝了"+drinkTol+"瓶酒");
System.out.println("剩"+leftBottles+"个空瓶");
}
} 运行结果:
共喝了455瓶酒
剩2个空瓶
{
if((beer+bottle) < 3) return beer;
return beer + Drink( (beer+bottle)/3, (beer+bottle)%3);
}
可以喝455瓶
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
class Program
{
public void sum(int i)
{
int s=0; // 总和
int n=i; //新酒
int empty=0; //空瓶
int newempty = 0; //新增空瓶
int newnew=0; //新增瓶子
while(n!=0)
{
s=s+n;
newempty= (n+empty)% 3;
newnew = (n+empty) / 3;
n = newnew;
empty = newempty;
System.Console.WriteLine("" + s + " " + n + " " + newempty);
}
}
static void Main(string[] args)
{
new Program().sum(304);
}
}
}
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()){
int x = scan.nextInt();
int sum = 0;
while(x>=3){
sum += x/3;
x = x-x/3*3+x/3;
}
System.out.println("empty bottle:\t"+x
+"\ntotal drunk:\t"+sum);
}
}
}
简单的题目,最后剩2个瓶子,喝了151瓶。
{
public static void main(String [] args)
{
int full=304; //满的啤酒
int empty=0; //空瓶
int drink=0; //喝了多少
while(full>0)
{
full-=1;
empty+=1;
if(empty==3)
{
empty=0;
full+=1;
}
drink+=1;
}
System.out.println("drunk:"+drink);
System.out.println("empty:"+empty);
}
}结果是 喝了一共455瓶,还剩2个空瓶