请教个问题,关于for循环的

hjx_gb2000 2008-08-07 11:11:59
李白无事街上走,提壶去买酒。遇店加一倍,见花喝一斗,五遇花和店,喝光壶中酒,试问李白壶中原有多少斗酒?(使用for循环结构实现)
题目好像我有点读不懂来着··
...全文
317 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
public static void libai(){
int wine;
int y;
int res;
for(wine=0;wine <10000;wine++){
for(int x=0;x <5;x++){
y=5-x;
res=x*(2*wine)-y*(wine-10);
if(res==0){
System.out.println(wine);
justinavril 2008-08-24
  • 打赏
  • 举报
回复
9#正解吧
import java.math.*;

public class Libai {
public static void main(String args[]){
BigDecimal volumn = new BigDecimal("0");

for (int i=0; i<5; i++){
volumn = volumn.add(new BigDecimal("1"));
volumn = volumn.divide(new BigDecimal("2"));
}

System.out.print(volumn);
}
}

0.96875
fskjb01 2008-08-24
  • 打赏
  • 举报
回复
public static void main(String[] args) {
// TODO Auto-generated method stub
double s=1d,sum;
for(int i=1;i<=5;i++){
s=s/2+1;
}
sum=s-1;
System.out.println("原来有:"+sum+"斗酒");
}
jacob_0812 2008-08-24
  • 打赏
  • 举报
回复
面试题 如果是三遇店和花 正确答案是7/8
jacob_0812 2008-08-24
  • 打赏
  • 举报
回复
面试题 如果是三遇店和花 正确答案是7/8
狂浪吻血 2008-08-24
  • 打赏
  • 举报
回复
import java.math.*;

public class libai {

public static void main(String[] args) {
BigDecimal x = new BigDecimal(0);
for (int i = 0; i < 5; i++) {
x = x.add(new BigDecimal(1));
x = x.divide(new BigDecimal(2));
}
System.out.println(x);
}



}
headyao 2008-08-24
  • 打赏
  • 举报
回复
有趣的题目
hjx_gb2000 2008-08-08
  • 打赏
  • 举报
回复
逆推是会了,但是如何用语句来实现呢,5楼和9的好像都不行嘛
fengwx007 2008-08-08
  • 打赏
  • 举报
回复
倒着推,觉得应该是店--花这样的顺序,因为这样最后才能“喝光壶中酒”。
0.96875斗酒。
fengwx007 2008-08-08
  • 打赏
  • 举报
回复
code=Java]
public class Practise {

/**
* @param args
*/
public static void main(String[] args) {
BigDecimal x = new BigDecimal(0);
for (int i = 0; i < 5; i++) {
x = x.add(new BigDecimal(1));
x = x.divide(new BigDecimal(2));
}
System.out.println(x);
}
}[/code]
hjx_gb2000 2008-08-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sd5816690 的回复:]
LZ是 彭某某 ?
主要是因为昨天他才问了我这个问题

[/Quote]

我不是彭某某,我是新手才来没几天呢。

这个题目主要是让我用for循环来做, 应该暂时不考虑花和店的顺序问题吧· C#我还没学呢·
tuoxie119 2008-08-08
  • 打赏
  • 举报
回复
倒着推,先遇到花,遇花加一斗,遇店喝一半
for(int i = 0 ; i < 10 ; i++){
if(i%2 == 0){}
else{}
}
zzz2121 2008-08-08
  • 打赏
  • 举报
回复
有点乱!
pj313306674 2008-08-08
  • 打赏
  • 举报
回复
===蜗牛小屋,编程开发,Java,j2se,j2me,j2ee,新手入门,高级技术,核心技术,资料,技术文章,个人收藏,开发笔记,网络编程 http://www.pjwqh.cn ===
TINY_LF841026 2008-08-08
  • 打赏
  • 举报
回复
package execise;

public class Libai {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

libai();
}

public static void libai(){
int wine;
int y;
int res;
for(wine=0;wine<10000;wine++){
for(int x=0;x<5;x++){
y=5-x;
res=x*(2*wine)-y*(wine-10);
if(res==0){
System.out.println(wine);
}
}}


}
}
写了一个,LZ看看答案对不?
fengwx007 2008-08-08
  • 打赏
  • 举报
回复
我的也不对?你搞错了吧!
原来0.96875

一遇店:0.96875*2 = 1.9375;
一遇花:1.9375-1 = 0.9375;
二遇店:0.96875*2 = 1.875;
二遇花:1.9375-1 = 0.875;
三遇店:0.96875*2 = 1.75;
三遇花:1.9375-1 = 0.75;
四遇店:0.96875*2 = 1.5;
四遇花:1.9375-1 = 0.5;
五遇店:0.96875*2 = 1;
五遇花:1.9375-1 = 0;

SARA520 2008-08-07
  • 打赏
  • 举报
回复
解应用题???
sd5816690 2008-08-07
  • 打赏
  • 举报
回复
LZ是 彭某某 ?
主要是因为昨天他才问了我这个问题

我用C#写的代码,可以显示出可能出现的情况(花和店的各种组合)
算出最终结果需要在下面的代码中加些东西

但下面这个方法效率很低,需要半分钟

至于2楼的朋友给出的答案,只是花和店出现顺序的一种

[code=C#]
using System;
using System.Collections.Generic;
using System.Text;

namespace Sort
{
public class Sort
{
private List<String> result = new List<String>();

private char[] chang(char[] str, int m)
{
int i = 0;
char temp = str[0];
for (i = 0; i < m - 1; i++)
{
str[i] = str[i + 1];
}
str[i] = temp;
return str;
}

private void pai(char[] str, int m, int n) /* 定义全排列函数 */
{
int k;
str = chang(str, m);
if (m < n) /* 定 义 递 归 调 用 出 口 */
{
for (k = 0; k <= m; k++)
{
pai(str, m + 1, n); /* 递归调用 */
chang(str, m); /* 调用左移函数 */
}
}
else
{
String tempResult = "";
for (int i = 0; i < str.Length; i++)
{
tempResult += str[i];
}
result.Add(tempResult);
}
}

// 这是得到全排列的方法
public List<string> showResult(String numbers)
{
char[] str = numbers.ToCharArray();
pai(str, 0, str.Length);
return result;
}
}

class Program
{
static void Main(string[] args)
{
Test("01");// 2种情况
Test("0011");// 6
Test("000111");// 20
Test("00001111");// 70
Test("0000011111");// 252
}

private static void Test(string str)
{
List<string> list = new Sort().showResult(str);// 全排列
List<string> list2 = new List<string>();// 最终结果

foreach (string var in list)
{
if (!list2.Contains(var))
{
list2.Add(var);
}
}
Console.WriteLine("\n" + list2.Count);
}
}
}


[/code]
xujiaxiang 2008-08-07
  • 打赏
  • 举报
回复
这个是3遇的,呵呵
xujiaxiang 2008-08-07
  • 打赏
  • 举报
回复
1。设原有酒X升。1斗=10升。
2。一遇店:X*2=2X。
3。一遇花:2X-10。
4。二遇店:(2X-10)*2。
5。二遇花:4X-20-10。
6。三遇店:(4X-30)*2-10。
7。三遇花:8X-60-10=0
8。解方程:8X=70,X=8。75升。李白壶中原有
8。75升酒。
壶中原有7/8斗酒.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧