传球游戏问题

mz126 2015-08-14 05:22:09
老师布置的题目。问题和解法把图贴在下面了。
我的问题是我的代码在模拟100000次的时候都是没有问题的,可是一旦模拟次数_round大于100000次的时候,比如1000000次的时候就会出现段错误。我调试的时候发现问题出现构造函数里面,可是还是不知道为什么_round大于100000的时候,程序就会崩溃。求大神指导。

问题描述:

问题解决:

代码实现:

/*************************************************************************
> File Name: Pass_ball.h
> Author: mz
> Mail:xx@qq.com
> Created Time: Wed 12 Aug 2015 09:20:22 AM CST
************************************************************************/
#ifndef GAME_Pass_ball_h
#define GAME_Pass_ball_h
#include <cstring>
#include <iostream> // for debug

namespace GAME
{

// 枚举类型声明
typedef enum{ LEFT , RIGHT } Choice;

// 类声明
class Pass_ball
{
public:

Pass_ball() : _index( 0 ) { init() ; ::memset( _arr , 0 , sizeof( _arr ) ); }

void run(); // 进行模拟

private:

static const int _round = 100000; // 模拟轮数
static const int _num_per_round = 10 ; // 每一轮传递次数
static const int _total = 5; // 传球人数

int _index; // 随机数表下标
char _choice_table[ _num_per_round * _round ]; // 随机数表
char _arr[ _total ]; // 传球队列

void init(); // 初始化随机数表

Choice get_choice(); // 获得向左或者向右传递的选择
int nrand( int n ); // 生成[0,n)之间的一个随机数

};// Pass_ball

}// GAME
/*************************************************************************
> File Name: Pass_ball.cpp
> Author: mz
> Mail:xx@qq.com
> Created Time: Wed 12 Aug 2015 09:25:40 AM CST
************************************************************************/
#include "Pass_ball.h"
#include <stdexcept>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <iomanip>

using std::domain_error; using std::cerr; using std::endl;
using std::cout; using std::setprecision;
using std::setiosflags; using std::ios_base;

namespace GAME
{

void Pass_ball::run()
{
int count = 0;

// 模拟round次传球
for( int cnt = 0 ; cnt != _round ; ++cnt )
{
int index = 0;
::memset( _arr , 0 , sizeof( _arr ) );

// 开始传球
for( int idx = 0 ; idx != _num_per_round ; ++idx )
{
_arr[ index ] = 0;

Choice choice = get_choice();
if( choice == LEFT )
{
index = ( index - 1 + _total ) % _total;
}
else
{
index = ( index + 1 ) % _total;
}

_arr[ index ] = 1;
}

if( 1 == _arr[ 0 ] ) ++count;
}

// 计算结果
double r = 1.0 * count / _round;
cout << setiosflags( ios_base::fixed ) << setprecision( 6 )
<< "ratio : " << r << endl;

}

void Pass_ball::init()
{
try
{
// 初始化随机数表
for( int i = 0 ; i < ( _num_per_round * _round ) ; ++i )
{
char x = (char)nrand( 2 );
_choice_table[ i ] = x;

}

}
catch( domain_error e )
{
cerr << e.what() << endl;
}
}

Choice Pass_ball::get_choice()
{
char x = _choice_table[ _index++ ];
if( 0 == x ) return LEFT;
else return RIGHT;
}

int Pass_ball::nrand( int n )
{
// 参数检查
if( n <= 0 || n > RAND_MAX )
{
throw domain_error( "Argument to nrand is invalid!" );
}

const int bucket_size = RAND_MAX / n;
int r = 0;

do r = rand() / bucket_size;
while( r >= n );

return r;

}

}//GAME
/*************************************************************************
> File Name: main.cpp
> Author: mz
> Mail:xx@qq.com
> Created Time: Wed 12 Aug 2015 11:36:28 AM CST
************************************************************************/
#include "Pass_ball.h"
using GAME::Pass_ball;

int main( void )
{
Pass_ball pb;

pb.run();

return 0;
}
#endif


...全文
163 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2015-08-14
  • 打赏
  • 举报
回复
引用 2 楼 a1193561652 的回复:
有可能是你的数组太大了,把它设置成全局的试试。
崩溃原因是数组_choice_table太大。修改方法可以是把_choice_table定义为指针,初始化时动态申请内存。 也可如下把pb移出main成为全局变量:
Pass_ball pb;

int main( void )
{	
	//移出 Pass_ball pb;

	pb.run();

	return 0;
}
707wk 2015-08-14
  • 打赏
  • 举报
回复
广度优先搜索?
纵横车 2015-08-14
  • 打赏
  • 举报
回复
有可能是你的数组太大了,把它设置成全局的试试。
赵4老师 2015-08-14
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 如果是递归,十有八九是堆栈溢出。

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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