67,514
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static void main(String[] args) throws Exception{
int bottle = 0;
for (int i = 1; i <= 27; i ++) {
System.out.println("第" + i + "个人喝到水啦!");
bottle ++;
if (bottle%3 == 0) {
i ++;
System.out.println("第" + i + "个人喝到免费水啦!");
}
}
System.out.println(bottle);
}
}
输出结果:
第1个人喝到水啦!
第2个人喝到水啦!
第3个人喝到水啦!
第4个人喝到免费水啦!
第5个人喝到水啦!
第6个人喝到水啦!
第7个人喝到水啦!
第8个人喝到免费水啦!
第9个人喝到水啦!
第10个人喝到水啦!
第11个人喝到水啦!
第12个人喝到免费水啦!
第13个人喝到水啦!
第14个人喝到水啦!
第15个人喝到水啦!
第16个人喝到免费水啦!
第17个人喝到水啦!
第18个人喝到水啦!
第19个人喝到水啦!
第20个人喝到免费水啦!
第21个人喝到水啦!
第22个人喝到水啦!
第23个人喝到水啦!
第24个人喝到免费水啦!
第25个人喝到水啦!
第26个人喝到水啦!
第27个人喝到水啦!
第28个人喝到免费水啦!
总共买了21瓶水。public static void main(String arg[]) {
int people = 27;
int sum = 0;
int flag = 0;
while (people >= 0) {
if (flag % 3 == 0) {
flag++;
people--;
}
sum++;
flag++;
people--;
}
System.out.println(sum);
}
public static void main(String[] args)
{
for(int i=1;i<27;i++)
{
if(count(i,0)==27)
{
System.out.println(i);
break;
}
}
}
private static int count(int x,int result)
{
result+=x;
if(x>=3)
{
result=count(x/3,result);
}
return result;
}
public
class Demo60 {
public static void main(String[] args) {
int i, c = 27;
for (i = (c / 3) << 1 ; m(i) < c; i++);
System.out.println(i);
}
static int m(int a) {
int d, o, m = a;
do {
m += d = a / 3;
o = a % 3;
a = d + o;
} while (d > 0);
return m;
}
}
public class Heshui {
private static int counter = 0;//新水计数
private static int emptyCount = 0;//空瓶计数
public static void main(String[] args) {
for (int i = 1; i <= 27; i++) {
drink(i);
}
System.out.println("至少需要"+counter+"瓶水");
}
public static void drink(int num){
System.out.println("第"+num+"个人喝水前空瓶数:"+emptyCount);
if(emptyCount > 2){//换新水
emptyCount -= 2;
System.out.println("第"+num+"个人用三个空瓶换了一瓶,喝水后空瓶数:"+emptyCount);
return;
}
//拿新水
counter ++;
emptyCount ++;
System.out.println("第"+num+"个人拿了一瓶新水,拿的新水数:"+counter+",喝水后空瓶数:"+emptyCount);
}
}
这样的话是至少19瓶水,不知道有没有忽漏public class Main {
public static void main(String[] arg) {
int people = 24;
int sum = 3;
int flag = 3;
while (people > 0) {
if (flag % 3 == 0) {
flag++;
people--;
}
sum++;
flag++;
people--;
}
System.out.println(sum);
}
}