16年软件杯 & 字符串读入处理 & 结构体指针初始化 & 随机函数

模拟车辆的程序

  • 一个车辆模拟的小程序
#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++ 编程规范

你可能感兴趣的:(16年软件杯 & 字符串读入处理 & 结构体指针初始化 & 随机函数)