frogwalk2.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 31 行

CPP
31
字号
#include <iostream>
using namespace std;
#include "prompt.h"
#include "walk.h"

// simulate two random walkers at once
// Owen Astrachan, 6/29/96, modified 5/1/99

int main()
{
	 int numSteps = PromptRange("enter # steps",0,30000);

	 RandomWalk frog(numSteps);       // define two random walkers
	 RandomWalk toad(numSteps);
	 int samePadCount = 0;            // # times at same location

	 frog.Init();                     // initialize both walks
	 toad.Init();

	 while (frog.HasMore() && toad.HasMore())
	 {   if (frog.Current() == toad.Current())
         {   samePadCount++;
	     }
	     frog.Next();
	     toad.Next();
	 }
	 cout << "frog position = " << frog.Position() << endl;
	 cout << "toad position = " << toad.Position() << endl;
	 cout << "# times at same location = " << samePadCount << endl;
	 return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?