猫吃老鼠的STL实现
来源:互联网 发布:淘宝最新评价规则 编辑:程序博客网 时间:2024/05/17 03:59
#include < vector >
#include < iostream >
using namespace std;
int eat(const int total, const int space)
{
if (total <= 0 || space <= 0)
{
return -1;
}
vector v;
vector::iterator it;
// 初使化向量
for (int i = 1; i <= total; ++i)
{
v.push_back(i);
}
int j = 1;
it = v.begin();
while (v.size() > 1)
{
// 如果数到第m个,则删除它,并且从头开始数
// 迭代器不需要后移,因为删除之后自动就指向下一个元素
if (space == j)
{
v.erase(it);
j = 1;
}
else
{
++it;
++j;
}
// 如果到最后了,让迭代器指向第一个元素
if (it >= v.end())
{
it = v.begin();
}
}
// 现在只有一个元素了,那么第一个元素就是我们需要的
return (*v.begin());
}
void main( void )
{
int total;
int space;
total = 5;
space = 1;
cout << "总共: " << total << " "
<< "间隔: " << space << " "
<< "最后: " << eat(total, space) << endl;
total = 5;
space = 2;
cout << "总共: " << total << " "
<< "间隔: " << space << " "
<< "最后: " << eat(total, space) << endl;
total = 5;
space = 3;
cout << "总共: " << total << " "
<< "间隔: " << space << " "
<< "最后: " << eat(total, space) << endl;
}
- 猫吃老鼠的STL实现
- 猫吃老鼠问题的链表实现
- C实现猫吃老鼠问题。。。。。
- 猫吃老鼠问题
- 猫吃老鼠
- 猫吃老鼠 2
- 猫吃老鼠问题
- 猫吃老鼠问题
- 猫吃老鼠算法
- 猫吃老鼠算法
- 面试题 猫吃老鼠
- 猫吃老鼠问题的单链表解法.
- 老鼠吃大米C#解法(递归实现)
- Java面试题:猫吃老鼠问题
- 笔试题1--猫吃老鼠问题
- 老鼠吃奶酪。
- 老鼠吃奶酪
- 方便面吃出老鼠 可怜的中国食品业啊。
- Hibernate容器映射技术(Set、List、Map)(1)
- 视空间知觉
- 使用BlazeDS实现Java和Flex通信之hello world
- 《Perl语言入门》第四版习题(5)
- Winsock frame in Win32 console application
- 猫吃老鼠的STL实现
- strncat 用法
- action标签
- 《Perl语言入门》第四版习题(4)
- ExtJS xtype class对照表
- 《Perl语言入门》第四版习题(3)
- Openfire Server presence(在线状态)消息处理流程
- 删除U盘时提示无法停止‘通用卷’设备的解决方法!!不要每次都硬拔了,对电脑有不小的损害!!!
- 食品购物是选择B2B?还是C2C?