如何编写c语言程序,模拟3d空间花粉布朗运动,输出x,y,z坐标即可
如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//获取某方向上的随机移动
int getPos(int posOld)
{
int direct = 1; //方向 1或者-1
int juli;//移动距离
int pos;
direct = rand() % 2; //生成0、1随机数
if (direct == 0) direct = -1; //如果是0,变成-1
juli = rand() % 10; //生成一个0-10的移动距离
pos = posOld + direct * juli; //某方向上的移动位置=原来的位置+随机移动方向*随机移动距离
return pos;
}
int main()
{
int i, tms = 100;//100次运动
int x, y, z;
srand((unsigned int)time(0)); //随机数种子
printf("请输入初始坐标x y z:");
scanf("%d %d %d", &x, &y, &z);
printf("%d,%d,%d\n", x, y, z);
for (i = 0; i < tms; i++)
{
x = getPos(x);
y = getPos(y);
z = getPos(z);
printf("%d,%d,%d\n", x, y, z);
}
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!