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

问题解决:

代码实现:
我的问题是我的代码在模拟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
...全文
相关推荐
对传球游戏的探讨 (2010年)
此类问题与传球游戏中的“传球”有些相似性,即球(或颜色)需要在一定条件下传递(或应用)。利用传球游戏得到的通项公式,可以推算出染色问题的解。
中班游戏:跨下传球.doc
这样的环节不仅锻炼了孩子们的反应速度,也培养了他们在面对困难时的解决问题的能力和团队间的相互协作。
算法-传球游戏(洛谷-P1057)(包含源程序).rar
**动态规划**:如果传球游戏的决策具有重叠子问题和最优子结构的特点,那么动态规划可能是理想的解决方案。动态规划通过存储和重用以前计算过的子问题答案来避免重复计算,从而提高效率。2.
大班体育游戏教案;.docx
【大班体育游戏教案】体育游戏在幼儿教育中扮演着重要的角色,它们不仅能锻炼孩子们的身体,提高他们的运动技能,还能促进他们的团队合作精神和解决问题的能力。以下是对几个体育游戏教案的详细解读:1.
大班体育游戏教案.doc
游戏不仅是对孩子们身体素质的训练,更是对他们的反应能力和解决问题能力的考验。在实践中,教师应注意观察孩子的跳跃姿势,适时给予指导,帮助他们掌握正确的跳跃方法。


