都是编程题(JAVA)
1.300瓶饮料喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
2.一个房子有1个门,2个窗户,50块地砖,其中门 100元/个, 窗户50元/个,地板砖 5元/块,请问总共花了多少钱?
1.300瓶饮料喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
2.一个房子有1个门,2个窗户,50块地砖,其中门 100元/个, 窗户50元/个,地板砖 5元/块,请问总共花了多少钱?
int num = 0;
while (true) {
left -= 3;
if (left>=0) {
num += 3;
left += 1;
}
else {
num += 3-left;
break;
}
}
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));
}
}
{
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)) ;
}
}
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 对不对?
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);
}
}
大家看看这个算法有什么问题??
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);
}
}
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 不知道对不对
所以总共可以喝:300 + 150 = 450 其实也就是:(300 + x)/ 3 = x
结果: 300 + x
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
{
// 最初有 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));
}
}
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
所以总共可以喝:300 + 150 = 450 其实也就是:(300 + x)/ 3 = x
结果: 300 + x============================这肯定是不对的 比如我现在手上有两瓶 喝完就没了 如果可以借 那就可以喝到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);
}
}
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;
我的程序最简单。
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
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 ;
}
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"吧?
首先300瓶喝了换掉又有100瓶,喝掉又换33瓶,多了一个空瓶,喝掉又换11瓶,在加上刚刚的空瓶,现在空瓶数为12喝掉换4瓶,在喝掉换1瓶,最后2瓶,不能换了
int result = 300;
while( n >= 3){
n = n - 3 + 1;
result ++;
}
System.out.print(result);449
{
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
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);
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啦
{
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问题解决了 通式
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
不知道对不对
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();
}
}
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
不做评论
{
if(n/3)==0;
return 0;
return n+drink(n/3);
}
用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);
}
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));
}
}有谁的算法和我的打印出来结果都一样的吗?
每次喝 m 瓶
每次喝完 m 瓶补偿 h瓶
公式:(n-h-(n-h)%(m-h))*m/(m-h)+h+(n-h)%(m-h)
{
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;
}
}
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;
}
}