汽水瓶
最后更新于:2022-04-01 16:17:40
<table id="table1" class="grid grid_tb " border="0" cellpadding="0" cellspacing="0" style="margin:0px; padding:0px; list-style-type:none; border-spacing:0px; border-collapse:collapse; max-width:100%; background-color:rgb(219,222,223); font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif; width:968px; border:0px; empty-cells:show; color:rgb(51,51,51); font-size:14px; line-height:20px"><tbody style="margin:0px; padding:0px; list-style-type:none"><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td width="10%" class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">描述: </td><td width="90%" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; list-style-type:none"><span style="margin:0px; padding:0px; list-style-type:none">有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?</span><br style="margin:0px; padding:0px; list-style-type:none"/></p> </td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">知识点:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"> 循环 </td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">题目来源:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"> 内部整理 </td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">练习阶段:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"> 中级 </td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">运行时间限制:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">无限制</td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">内存限制:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">无限制</td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">输入:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; list-style-type:none"><span style="margin:0px; padding:0px; list-style-type:none">输入文件最多包含10组测试数据,每个数据占一行,仅包含一个正整数n(1<=n<=100),表示小张手上的空汽水瓶数。n=0表示输入结束,你的程序不应当处理这一行。</span><br style="margin:0px; padding:0px; list-style-type:none"/></p> </td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">输出:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"> <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; list-style-type:none"><span style="margin:0px; padding:0px; list-style-type:none">对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。</span><br style="margin:0px; padding:0px; list-style-type:none"/></p> </td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">样例输入:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"><pre style="margin-top:0px; margin-bottom:10px; padding:9.5px; list-style-type:none; white-space:pre-wrap; word-wrap:break-word; line-height:1.42857143; overflow:auto; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:13px; word-break:break-all; background-color:rgb(245,245,245); border:1px solid rgb(204,204,204)">3
10
81
0
</pre></td></tr><tr style="margin:0px; padding:0px; list-style-type:none; height:26px"><td class="grid_left_td" style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; border-right-style:solid; border-right-width:1px; border-right-color:rgb(238,238,238); font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)">样例输出:</td><td style="margin:0px; padding:6px 3px 3px; list-style-type:none; word-break:break-all; word-wrap:break-word; font-family:'Microsoft YaHei',Arial,Helvetica,sans-serif!important; font-size:14px; border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:rgb(238,238,238)"><pre style="margin-top:0px; margin-bottom:10px; padding:9.5px; list-style-type:none; white-space:pre-wrap; word-wrap:break-word; line-height:1.42857143; overflow:auto; font-family:Menlo,Monaco,Consolas,'Courier New',monospace; font-size:13px; word-break:break-all; background-color:rgb(245,245,245); border:1px solid rgb(204,204,204)">1
5
40
</pre></td></tr></tbody></table>
两种方式处理,注意下面代码中输入不确定的n个数以0结束输入的方法:
~~~
//方法一:找到规律就这么简单n/2
void fun1() {
int n;
//注意这里输入不确定个整数,以0结束的输入方式
while (cin >> n, n) {
if (n < 0)
break;
cout << n / 2 << endl;
}
}
//方法二:
int fun2(int n) {
int remainder = 0;
int divisor = 0;
int count = 0;
while (n / 3) {
divisor = n / 3;
remainder = n % 3;
count += divisor;
n = divisor + remainder;
}
//如果最后还剩两个空瓶子,可以向老板借一个
//空瓶子再买一瓶
if (n % 3 == 2)
count += 1;
return count;
}
int main() {
//fun1();
int n;
while (cin >> n) {
if (n != 0) {
cout << fun2() << endl;
}
else
break;
}
return 0;
}
~~~