🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
//原题目:0出发,回到0成功,到达N(North)则失败:;求成功和失败的概率 之比例; ``` // // main.cpp // leftOrRight210801 //南极0出发,回到0南极则成功;到达N北极掉落 // Created by terry on 2021/8/19. //Start from the Zero(South),go back Zero is Success, and go Fail N(North) is Fail! #include #define times01 1000000 //电脑快可以设置更多,比如百亿次实验 //#define NN 3 **int** NN1=500; **int** main(**int** argc, **const** **char** \* argv\[\]) { // insert code here... // **int** pIn=0; **int** zeroSuccess=0; //Success Come bak Zero **int** NorthFail=0; //Fail to the North WuQiong **bool** tmpbool; //Bool 0 or 1 **for** (**unsigned** **long** **int** ii=0;ii<times01;++ii) {//for110ii pIn=0; //初始化 ++pIn; //激活 右Right Jump one Time(s); // **do** { //do220 Start110: tmpbool=rand()%2; **if**(1\==tmpbool) {//if220 //1== Right Jump; ++pIn; **if**(pIn>=NN1) { NorthFail++; **goto** North880Fail; } **else** {**goto** Start110;} //这个 else 省略 的话,不影响逻辑 }//if220 **else** **if** (0\==tmpbool) {//if330elseIf \--pIn; **if**(pIn<1) {++zeroSuccess; **goto** Zero90ZeroSuccess;} **else** {**goto** Start110;} //这个 else 省略 也不影响逻辑 }//if330elseIf **else** { std::cout<<"Error990!";} //这个 else 是冗余的 (多余的) } **while** (pIn>0 && pIn<=NN1); //do220 //这个条件 也是 冗余的 // North880Fail:; Zero90ZeroSuccess:; }//for110ii // std::cout<<"When the Num of Cells总格子数:NN="<< NN1<<":"<<std::endl; std::cout<<"Run:"<< times01<<" times experiments!"<<std::endl; std::cout<<"回到0的次数Nums go home to Zero Success:"<<zeroSuccess<<std::endl; std::cout<<"Fail to N,掉落于N(North):"<<NorthFail<<std::endl; **return** 0; //这句 冗余, 是固定格式… }//main() ```