twodwalk.cpp
来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 33 行
CPP
33 行
#include <iostream>
using namespace std;
#include "prompt.h"
#include "walk2d.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);
RandomWalk2D frog(numSteps,1); // define two random walkers
RandomWalk2D toad(numSteps,1);
int samePadCount = 0; // # times at same location
frog.Init(); // initialize both walks
toad.Init();
while (frog.HasMore() && toad.HasMore())
{ // if (frog.Current() == toad.Current())
if (frog.Current().distanceFrom(toad.Current()) < 1.0)
{ 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 + -
显示快捷键?