<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;
}
~~~
- 前言
- Josephus约瑟夫问题及其变种
- 链表的常见实现
- 二叉树遍历、插入、删除等常见操作
- 二叉堆的插入删除等操作C++实现
- 插入排序和希尔排序
- 堆排序
- 归并排序及其空间复杂度的思考
- 快速排序的几种常见实现及其性能对比
- 红黑树操作及实现
- 整数的二进制表示中1的个数
- 位操作实现加减乘除四则运算
- 冒泡排序的改进
- 直接选择排序
- 不借助变量交换两个数
- 基础排序算法总结
- AVL树(Adelson-Velskii-Landis tree)
- avl树的C++实现
- 动态规划之钢条分割
- hash函数的基本知识
- 动态规划:求最长公共子串/最长公共子序列
- 最长递增子序列
- 称砝码问题
- 汽水瓶
- 字符串合并处理(二进制位的倒序)
- 动态规划:计算字符串相似度
- m个苹果放入n个盘子
- 生成k个小于n的互不相同的随机数
- 栈和队列的相互模拟
- 字符串的排列/组合
- KMP(Knuth-Morris-Pratt)算法
- n个骰子的点数
- 位运算的常见操作和题目