传球游戏问题

mz126 mz126 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
老师布置的题目。问题和解法把图贴在下面了。
我的问题是我的代码在模拟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


...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
mz126
C++ 语言 65,214 成员 250,514 内容