都是编程题(JAVA)
1.300瓶饮料喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
2.一个房子有1个门,2个窗户,50块地砖,其中门  100元/个, 窗户50元/个,地板砖 5元/块,请问总共花了多少钱?

解决方案 »

  1.   

    int left = 300;
    int num = 0;
    while (true) {
      left -= 3;
      if (left>=0) {
        num += 3;
        left += 1;
      }
      else {
        num += 3-left;
        break;
      }
    }
      

  2.   

    public class Test {
    int drink(int n){
    if(n<3)return 0;
    return n+n%3+drink(n/3);
    }
    public static void main(String[] args){
    System.out.println(new Test().drink(300));
    }
    }
      

  3.   

    有没有规定多少时间内做出来啊?class Drink
    {
      public String count(int start)
       {
         int total = 0, empty =0;
         while(true)
          {
             total += start;
             empty = start%3 + start/3;
              if(empty < 3)
               {
                 break;             
               }
              else
               {           
                 start = empty;
                }
          }
         return "总共喝了" + total + "瓶,最后还剩" + empty + "个空瓶!";
       }  public static void main(String[] args)
      {
        int initstart =300;
        Drink drk = new Drink();
        System.out.println(drk.count(initstart)) ;
      }
    }
      

  4.   

    public class BottlePro{
    public static void main(String[] args){
    int bottle=300;
    int drank=0;
    int sum=0;
    for (;bottle>0;bottle=bottle-1){
    sum=sum+1;
    drank=drank+1;
    if(sum==3){
    drank=drank+1;
    sum=1;
    }
    }
    System.out.println("Finally you have drank"+drank);
    }
    }
    最后结果449 对不对?
      

  5.   

    public class Test{

    public static void main(String[] args){
    int left ;
    int num=0;
    for(left=300;left>0;left--,num++)
      {if (num%3==0) 
    {
         left++;
       }
      
      }System.out.println(num);
    }
    }
    大家看看这个算法有什么问题??
      

  6.   

    public class Test{
    public static void main(String []argv){
    int z = 300;
    int y = 3;
    int x = 0;
    while(z >= 3)
    {
    z -= y;
    x += y;
    z += 1;
    }
    x += z;

    System.out.println(x);
    }
    }
      

  7.   

    public class bottle{
    public static void main(String[]args){
    int total=300;
    int drink=0;
    int count=0;
    while(total>0){
    total--;
    drink++;
    count++;
    if(count==3){
    total++;
    count=0;
    }

    }
    System.out.println("Answer is:"+drink);


    }
    }执行完是449   不知道对不对
      

  8.   

    1题: 喝300瓶 --> 300 个空瓶-->借 150 空瓶 --> 换150瓶 - > 喝完它,还给别人.
    所以总共可以喝:300 + 150 = 450 其实也就是:(300 + x)/ 3 = x
          结果: 300 + x
      

  9.   

    public class Test001 {
        public static void main(String args[]) {
            int total=300;
            int bottle=total,drink=total;
            do{
                drink+=total/3;
                bottle=total/3+total%3;           
                total=bottle;
            }while(bottle>=3);
            if(bottle==2)//剩2个瓶时,可向卖家多借一个,凑够三瓶再换一瓶。
                drink++;
            System.out.println("总共可以喝:"+drink);
        }
    }
    执行结果是450
      

  10.   

    public class T
    {
    // 最初有 ori 瓶, ex 个空瓶可以换一瓶
    public static int total(int ori,int ex)
    {
    if (ex > 1)
    return  ori + ori/(ex - 1);
    else if (ex == 1)
    return ori + ori;

    return Integer.MAX_VALUE;
    }

    public static void main(String[] args)
    {
    System.out.println(total(300,3));
    }
    }
      

  11.   

    public class BottlePro{
    public static void main(String[] args){
    int bottle=300;
    int drank=0;
    int sum=0;
    for (;bottle>=0;bottle=bottle-1){
    sum=sum+1;
    drank=drank+1;
    if(sum==3){
    drank=drank+1;
    sum=1;
    }
    }
    System.out.println("Finally you have drank"+drank);
    }
    }
    这样结果是451
      

  12.   

    1题: 喝300瓶 --> 300 个空瓶-->借 150 空瓶 --> 换150瓶 - > 喝完它,还给别人.
    所以总共可以喝:300 + 150 = 450 其实也就是:(300 + x)/ 3 = x
          结果: 300 + x============================这肯定是不对的    比如我现在手上有两瓶    喝完就没了      如果可以借 那就可以喝到3瓶
      

  13.   

    /*1.300瓶饮料喝完3个空瓶可以换一瓶饮料,依次类推,请问总
    共喝了多少瓶饮料?*/import java.util.*;
    public class count{
           static int countnumber(int n){
    if(n<3)return n;
    else  return (n+countnumber(n/3)); }
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    System.out.println("please input a number");
                    int i=sc.nextInt();
    int j=countnumber(i);
    System.out.println(j);
    }
    }     
      

  14.   

    package com.test;public class Test {
        public Test() {
        }    public static void main(String[] args) {
           int sum=300;
           int counnt=0;
          while(sum>2){
               counnt+=3;
               sum-=2;          
           }
           System.out.println(counnt+sum);
        
       }    
        
    }
    最后正确结果是449;
    我的程序最简单。
      

  15.   

    #include <iostream>
    using namespace std;
    int main()
    {
        int i=300;
    int count=0;
    while(i>0)
    {
    for(int j=1;j<=3;j++,count++,i--)
    if(i<=0) break;
    else if(j==3&&i>=0)
    i++;
    }
    cout<<count;
        return 0;
    }
    449
      

  16.   

    spring+struts+hibernate学习讨论技术群9967568,欢迎爱好者交流探讨
      

  17.   

    傻瓜,应该是446瓶。图解如下:
    3 3 3 3 3 3 3 3 3 3    
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 换了100喝了300 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 
    3 3 3 3 3 3 3 3 3 3 换了30喝了99  余1
    3 3 3 3 3 3 3 3 3 3 
    换了10瓶,喝了30瓶3 3 3 
    换了3瓶,喝了9瓶, 余1
    3
    换了1瓶 喝了3瓶   换来的未喝,余下1瓶总共余下3瓶,又可以换1瓶,喝了3瓶, 有余下1瓶,未喝。code[c]:#include <iostream.h>
    #include <stdio.h>
    #define true 1 void main()
    {
    int left = 300;
    int num = 0;
    while(true) 
    {
      left -= 3; //每次喝掉3瓶
      if(left>=0) 
      {
    num += 3; //喝掉的总和
    left += 1; //一共有的数量
      }
      else 
      {
    num += left;
    break;
      }
    }

    printf("%d",num); return ; 
    }
      

  18.   

    int drinked = 0;
    for(int count = 300;count>0;count--)
    {
         drinked++;
         if(drinked%3==0&&drinked>0)
         {
    count++;
         }
    }结果 drinked = 449 对否?to:0011411(爱也许是假的,可是当时的快乐是真的。)
    "换了30喝了99  余1"
    应该是 "换了33喝了99  余1"吧?
      

  19.   

    我心里有算法,因为现在还没有学习JAVA,正确答案451。
    首先300瓶喝了换掉又有100瓶,喝掉又换33瓶,多了一个空瓶,喝掉又换11瓶,在加上刚刚的空瓶,现在空瓶数为12喝掉换4瓶,在喝掉换1瓶,最后2瓶,不能换了
      

  20.   

    int n = 300;
        int result = 300;
        while( n >= 3){
          n = n - 3 + 1;
          result ++;
        }
        System.out.print(result);449
      

  21.   

    class Drink
    {
        public static void main(String [] args)
        {
            int sum;     
            int drink = 0;
            for(sum=300;sum>2;)
                {
                    sum-=2;
                    drink += 3;
                }
             
                if(sum<=2&&sum>0)
                {
                    drink+=sum;
                }
              n=drink/3;
              System.out.println("共可以喝"+drink+"瓶饮料"); 
              System.out.println("最后剩下"+sum+"个空瓶子");
              System.out.println("共兑换"+n+"次");
        }
    }drink: 449     sum:2    n:149
      

  22.   

    public class drink {

    public drink(int bottle) {
        int num=0,total;
            int left=bottle;
        while(left>=3)
        {
         left-=2;
         num+=1;
        }
        if(left==2)
         num++;
        total=bottle+num;
        System.out.println("You have drinked "+total+" wine!");
    }
    public static void main(String[] args) {
    new drink(300);
      

  23.   

    还是c习惯点....int fullbottle_num = 300;
    int emptybottle_num = 0;
    int drunk_num = 0;  while( fullbottle_num > 0)
    {
    drunk_num += fullbottle_num;             //喝掉
    emptybottle_num += fullbottle_num;       //变空瓶


    fullbottle_num = emptybottle_num /3;     //空瓶换满的
    emptybottle_num = emptybottle_num % 3;   //不能换的空瓶
    }printf("%d\n",drunk_num);
    449啦
      

  24.   

    public class Test
    {
    public void aa(int m,int n)
    {
    int temp=0;
    int sum=m;
    String msg;
    while (true)
    {   
        int next=(int)(m/n);
        sum=sum+next;
    temp=m%n;
    m=next+temp;
    if(m<n)
    {
    if(m==n-1)
    msg="最后剩下"+(n-1)+"个瓶,找人借一个瓶喝完后还给他总共喝"+(sum+1)+"瓶";
    else
    msg="总共喝下"+sum+"瓶,还剩下"+temp+"个空瓶";

    break;
    }
    }

    System.out.println(msg);
    }

    public static void main(String args[])
    {
    new Test().aa(300,3);
    }
    }
    方法aa接收两个参数 m瓶汽水 n个空瓶换一瓶汽水大家可以运行式哈子OK问题解决了  通式
      

  25.   

    class BottleNum{
    int all = 300;//总的喝掉的数
    int num = 100;//可以兑换的数
    int kon = 0;//不能换的数
    int aaa = 300;//换好的数
    void test(){
    while(num>2){
    num = (aaa+kon)/3;
    kon = (aaa+kon)%3;
    aaa = num;
    all = all + num;
    num = num + kon;
    }
    System.out.println(all);
    }
    public static void main(String[] args){
    BottleNum b = new BottleNum();
    b.test();
    }
    }这是本人的算法
    最终答案是449
    不知道对不对
      

  26.   

    public class Pth {
    public Pth() {
    int x = 0;
    int i = 3;
    while (i < 300 + x) {
    i += 3;
    x++;
    }
    System.out.println("共喝 " + x + 300 + " 瓶");
    } public static void main(String[] args) {
    new Pth();
    }
    }
      

  27.   

    题目要用Java做,估计想试下你能不能用OO把这个程序写了吧~~而不是考你算数
      

  28.   

    差不多吧,这是我的算法:
    public class Drink{
    public static void main(String[] args){
    int remain = 300;//剩下的饮料数目
    int total = 0;
    int flag = 0;
    while(init>0){
    --remain;
    ++flag;
    ++total;
    if(flag==3){
    ++remain;
    flag = 0;
    }

    }
    System.out.println(total);
    }
    }结果:449
    不做评论
      

  29.   

    int drink(int n)
    {
       if(n/3)==0;
           return 0;
       return n+drink(n/3);
    }
      

  30.   

    第二题,直接return 450;好了。
      

  31.   

    449吧~
    用C写了一下~~
    #include <stdio.h>main()
    {
      int Num=300;
      int m,n,rst=Num;
      
      while(Num>=3)
        {
           m=Num/3;
           n=Num%3;
           rst=rest+m;
           Num=m+n;
         };
      
       printf("%d\n",rst);
    }
      

  32.   

    又想了一下,应该是这样
    class Test
    {
        public int drink(int n)
        {
           return drink(n,0);
         }
        public int drink(int f,int e)
        {
          if((f+e)<3)
              return f;
           return f+e/3+drink(f/3,f%3+e%3);
         }
         public static void main(String args[])
         {
            for(int i=300;i<330;i++)
                   System.out.println(i+":"+drink(i));
          }
    }有谁的算法和我的打印出来结果都一样的吗?
      

  33.   

    饮料总数设为 n
    每次喝 m 瓶
    每次喝完 m 瓶补偿 h瓶
    公式:(n-h-(n-h)%(m-h))*m/(m-h)+h+(n-h)%(m-h)
      

  34.   

    public class TestCount
    {
    public static void main(String [] args)
    {
    System.out.println(count(300));
    } public static int count(int i)
    {
    if(i>=3)
    return i+count(i/3);
    else
    return 0;
    }
    }
      

  35.   

    上面错了,我再发过。
    public class TestCount
    {
    public static void main(String [] args)
    {
    System.out.println(count(300));
    } public static int count(int i)
    {
    if(i>=3)
    return i+count(i/3);
    else
    return i;
    }
    }