有50瓶饮料,喝完后每三个空瓶子可以换一瓶,问总共可以喝多少瓶饮料用JAVA写一个程序

有50瓶饮料,喝完后每三个空瓶子可以换一瓶,问总共可以喝多少瓶饮料用JAVA写一个程序,第1张

int bdrink(int drink,int empty_bottle,int full_bottle)//drink表示喝的瓶数,empty_bottle是空瓶数,full_bottle是满瓶数

{

if(full_bottle>0)//当可以换到满瓶时

{

drink+=full_bottle;

empty_bottle+=full_bottle;

full_bottle=empty_bottle/3;

empty_bottle-=(full_bottle3);

return bdrink(drink,empty_bottle,full_bottle);

}

else//不能换到满瓶饮料

{

if(empty_bottle==2)//如果剩余空瓶为2,可以通过借一个空瓶换一个满瓶,喝完还回去的方法多喝一瓶

return drink+1;

else

return drink;

}

}

调用是bdrink(0,0,50);

public class Test {

    public static void main(String[] args) {

        int drinks = 1000; // 饮料的数量

        int bottle = 0; // 当前空瓶的数量

        int sum = 0; // 总共喝的饮料数

        // 当饮料数量大于0 或者 空瓶数量大于等于 3 就可以继续喝或者换饮料

        while (drinks > 0 || bottle >= 3) {

            if (drinks > 0) { // 喝光饮料

                sum = sum + drinks;

                bottle = bottle + drinks;

                drinks = 0;

            }

            if (bottle >= 3) { // 空瓶换饮料

                drinks = bottle / 3; // 换的饮料数

                bottle = bottle % 3; // 剩下的空瓶数

            }

        }

        Systemoutprintln("总共喝的饮料数量为:" + sum);

    }

}

总共喝的饮料数量为:1499

1java创建一个类,他的功能是产生一个100以内随机正整数,并打印出来(提示:使用Random类是吧?

import javautil;

public class Q1 {

Random random;//Random 类是javautil包中的

int toPrint;//被赋值为随机数后要被打印的变量

Q1(){

toPrint = (int) ((int)100randomnextDouble());

//通过random的nextDoutle方法获得一个00~10的double数,

//想要一个1~100的整数,所以要乘以100后再类型转换

}

///////////这是要打印随机数的方法

public void printRando(){

Systemoutprintln(toPrint);

}

//如果你只要个类,那么以上就可以了,这个主函数入口是为了看结果用

public static void main(String[] args){

Q1 q = new Q1();//创建Rando对象

qprintRando();//调用其打印随机数的方法

}

}

2小明花1块1毛钱,买了一瓶汽水,他给了2块钱,打印出应该找多少钱(提示:用BigDecimal类)

import javamathBigDecimal;

public class Q2 {

BigDecimal pay;//付的钱数

BigDecimal cost;//货物价格

BigDecimal back;//找回的钱

public void setCost(String strcost){

cost = new BigDecimal(strcost);

}////给货物价格赋值

public void setPayment(String strpay){

pay = new BigDecimal(strpay);

}////给付的钱赋值

public double getBack(){

return thispaysubtract(thiscost)doubleValue();

}//得到找零

public void XiaoMingBuy(){

thissetCost("11");

thissetPayment("20");

Systemoutprintln(thisgetBack());//打印找零

}

public static void main(String[] args){

new Q2()XiaoMingBuy();

}

}

#include <stdioh>

int main()

{

int t,n, k, total;

scanf("%d",&t);

while (t--)

{

scanf("%d %d", &n, &k);

total = 0;

int count=n;

while (count >= k)

{

total += count/k;

count/=k;

}

printf("%d\n", total+n);

}

return 0;

}

以上就是关于有50瓶饮料,喝完后每三个空瓶子可以换一瓶,问总共可以喝多少瓶饮料用JAVA写一个程序全部的内容,包括:有50瓶饮料,喝完后每三个空瓶子可以换一瓶,问总共可以喝多少瓶饮料用JAVA写一个程序、1000瓶饮料,3个空瓶换一瓶,java实现、java创建一个类,他的功能是产生一个100以内随机正整数,并打印出来(提示:使用Rando等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10179661.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存