一个简单的算法问题
一块钱一瓶水 两个瓶子可以换一瓶水 给你100块钱 可以喝多少瓶水
用C# 实现
[解决办法]
int result = 100 * 2 - 1;
[解决办法]
int nPingZiCount=100;//每次瓶子的数目,最开始是100
int nResult=0;//可以喝的水数目
do
{
nResult+=nPingZiCount;
nPingZiCount=nPingZiCount/2;
}
while(nPingZiCount>0);
//结果在nResult中,
[解决办法]
确切地说是
int n = 100 / 1;
int result = n * 2 - 1;
[解决办法]
<script type="text/javascript"> var howMuchBottle = 0; function count(totalMoney) { var i = 1; while (i < totalMoney && i != totalMoney) { howMuchBottle += Math.floor(totalMoney / i); i *= 2; } return howMuchBottle++; } alert(count(20)); </script>
[解决办法]
int i = 100; //瓶子数 int sum = 0; //喝到多少瓶水 sum = i; while (i > 1) { sum += i / 2; i = i % 2 + i / 2; }
[解决办法]
int nCount = money; int nResult = 0; nResult += nCount; while (nCount > 1) { nCount -= 2; nResult++; nCount += 1; } Console.WriteLine(nResult);
[解决办法]
这个问题好像出现过很多次了
[解决办法]
想要什么样的解法啊。很多种的。
程序算法:
int kp=100; int he=100; int nhe=0; do {nhe=kp/2;he+=nhe;kp=kp/2+kp%2; } while(kp>=2); printf("%d\n",he);
[解决办法]
Lz结贴是正道!楼上都答完了,LZ你还想得到什么样的答案你补充啊!
[解决办法]
学习了 啊 呵呵 虽然。。。。
[解决办法]
按上面有几位贴出的代码结果应当都是199瓶吧。但如果换一种思考方式:两个瓶子换一瓶水,这就相当于瓶子是0.5元一个。如果我从人家哪借来100元,然后全部买200瓶。喝完后出来200个瓶子,然后把瓶子(0.5元/个)换成钱,刚好是100元,然后还人钱。这不是200瓶吗?问题出在哪?
[解决办法]
理论是199瓶,实际可以200瓶,最后剩下的那个空瓶的时候,和老板借一个空瓶然后换一瓶水,喝了以后再把空瓶还给老板。