C++漫步者问题模拟
来源:互联网 发布:pptv网络电视在线 编辑:程序博客网 时间:2024/05/17 05:54
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
//随机漫步问题
class Vector
{
private:
double mag;//极坐标长度
double dir;//极坐标方向
public:
Vector();
Vector(double m,double d);
double magval()
{
return mag;
}
double dirval()
{
return dir;
}
Vector operator+(const Vector &v)
{
//先化作直角坐标
float x=mag*cos(dir);
float y=mag*sin(dir);
float v_x=v.mag*cos(v.dir);
float v_y=v.mag*sin(v.dir);
float x_total=x+v_x;
float y_total=y+v_y;
return Vector(sqrt((x_total*x_total+y_total*y_total)),atan2(y_total,x_total));
}
};
Vector::Vector()
{
mag=0;
dir=0;
}
Vector::Vector(double m,double d)
{
mag=m;
dir=d;
}
int main()
{
srand(time(0));
Vector result(0.0,0.0);
double direction;
int steps=0;
//直至漫步到距离为50的地方才停止
while(result.magval()<50)
{
//随机生成指定步数的向量
direction=rand()%360;
Vector temp(2,direction);
//走一步
result=result+temp;
steps++;
}
cout<<"steps="<<steps<<endl;
return 0;
}
- C++漫步者问题模拟
- C程序模拟停车场管理问题
- 狼追兔子问题C模拟解
- 模拟题 c/c++ 换酒喝问题
- c 模拟 httpget,主要是发送的数据的问题
- 使用C++TR1实现物流配送问题的简单模拟
- PAT 2-09 装箱问题模拟(C语言实现)
- July 14th 模拟赛C T2 数码问题 Solution
- 问题 C: 序列求平均(简单模拟)
- C语言 竖式问题 模拟竖式
- 浙大模拟(1)问题 C: 还原二叉树
- C/C++广度优先搜索模拟迷宫求解问题
- PVCBOT【24号】漫步者--步履机械马
- 孤单漫步者的遐想@7空白格
- 月球漫步者—迈克尔。杰克逊
- 模拟问题
- C 模拟词法分析
- .NET (c#) 模拟 Cookie
- SQLSERVER id in string
- mac杂文
- android中黑名单的实现
- 将文件隐藏到图片中
- C++友元函数
- C++漫步者问题模拟
- C++ char强行转换成指针
- C++<<重载
- 实现vmware workstation内linux访问宿主机win7上的文件夹
- C++多态思想
- valarray c++ 用法
- C#中委托的理解(方便Java的童孩们理解)
- python字符串翻转
- python批量自动化工作