16年软件杯 & 字符串读入处理 & 结构体指针初始化 & 随机函数
来源:互联网 发布:诺基亚按键机java游戏 编辑:程序博客网 时间:2024/05/29 02:55
模拟车辆的程序
- 一个车辆模拟的小程序
#include <iostream>#include <cstdio>#include <cstring>#include <time.h>#include <stdlib.h>using namespace std;// 20km/h - 60km/h 108s之内(20km/h跑600m)肯定会拍到一次,30s(60km/h跑500m)之内不会拍到第二次,5min/300s内最多有10次出镜机会// 600m的距离至少需要36s, 500米距离最多需要90s// 20万辆车有 200万次记录typedef struct tagDIRECTION{ int x_offset; int y_offset;} DIRECTION;DIRECTION direction_4[] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; // 按照 上下左右 的顺序排列typedef struct{ int x, y; //长40个卡口间距500(水平),宽40个卡口间距600(垂直)的一个矩形, int time;} Position;typedef struct{ int num; char str[8]; Position pos[15];} Car;void getstring(char *test){ int count = 0; while((test[count] = getchar()) != ',') ++count; test[count] = '\0'; char c = getchar(); if(c == '0'){ getchar(); getchar(); return; } count = 0; test[count] = c; while((test[++count] = getchar()) != '\n'); test[count] = '\0';}int main(){ freopen("car.txt", "r", stdin); cout << "编号" << " " << "记录时间" << " " << "位置x" << " " << "位置y" << endl; Car car[20000]; int tmp; // 生成时间随机数的变量 int totalTime = 0; //5分钟内的计时时间 for(int i = 0; i < 200000; i++) { // 对每辆车进行初始化 getstring(car[i].str); car[i].num = i + 1; tmp = rand() % 78 + 30; // 第一次出现的时间 car[i].pos[0].time = tmp; totalTime += tmp; car[i].pos[0].x = rand() % 40; car[i].pos[0].y = rand() % 40; //第一次出现的位置// cout << car[i].num << " " << car[i].pos[0].time << " " << car[i].pos[0].x * 500 << " " << car[i].pos[0].y * 600 << endl; cout << car[i].num << " " << car[i].pos[0].time << " " << car[i].pos[0].x << " " << car[i].pos[0].y << endl; // 对每辆车进行5分钟内的模拟 int dir; //下一步的方向 int idx = 1; // 第几次被拍到 while(totalTime <= 300){ tmp = rand() % 78 + 30; // 和上一次记录的时间间隔 totalTime += tmp; // 5分钟内的总时间 if(totalTime > 300) break; if(tmp < 36) dir = rand() % 2 + 2; else if(tmp > 90) dir = rand() % 2; // 600m的距离至少需要36s, 500米距离最多需要90s else dir = rand() % 4; // 行驶的方向 car[i].pos[idx].time = totalTime; car[i].pos[idx].x = car[i].pos[idx - 1].x + direction_4[dir].x_offset; car[i].pos[idx].y = car[i].pos[idx - 1].y + direction_4[dir].y_offset; if(car[i].pos[idx].x < 0 || car[i].pos[idx].x >= 40) car[i].pos[idx].x = car[i].pos[idx - 1].x - direction_4[dir].x_offset; if(car[i].pos[idx].y < 0 || car[i].pos[idx].y >= 40) car[i].pos[idx].y = car[i].pos[idx - 1].y - direction_4[dir].y_offset; cout << car[i].num << " " << car[i].pos[idx].time << " " << car[i].pos[idx].x << " " << car[i].pos[idx].y << endl;// cout << car[i].num << " " << car[i].pos[idx].time << " " << car[i].pos[idx].x * 500 << " " << car[i].pos[idx].y * 600 << endl; idx ++; } totalTime = 0; cout << endl; } return 0;}
数据库插入中间件
- 见 c++ 编程规范
0 0
- 16年软件杯 & 字符串读入处理 & 结构体指针初始化 & 随机函数
- 结构体函数指针的初始化
- 结构体指针初始化
- 字符串数组初始化、处理函数
- 07指针 指针与数组 指针与字符串 指针与函数 结构体与指针 宏
- 结构体指针的初始化
- 结构体指针初始化错误
- 结构体指针初始化问题
- Python Ctypes结构体指针处理(函数参数,函数返回)
- 结构体初始化及结构体指针.结构体数组.结构体函数的调用赋值等
- 函数指针,结构体指针
- 指针与字符串_字符串处理函数
- 结构体指针函数
- 结构体、函数指针
- 怎样用函数初始化结构体
- c语言结构体指针初始化
- 结构体中含有指针如何初始化
- c语言结构体指针初始化
- 第十三周实践项目2.3-动物这样叫
- vim配置及插件安装管理(超级详细)
- 数据清洗的一些梳理
- HTML获取触发事件的元素id
- ios JSON NSString to NSMutableDictionary
- 16年软件杯 & 字符串读入处理 & 结构体指针初始化 & 随机函数
- Erlang gen_tcp相关问题汇编索引
- leetcode.138. Copy List with Random Pointer
- 洛谷P1040
- Python-文件对象常用的API
- java实现文件下载的几种情况
- highcharter R语言
- FIRST集、FOLLOW集和SELECT集
- uva 10553 Treasure Map