假设某段马路的指定停车区间为 [0,6] ,车长L为2,随机停车 ...
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int L; //车长 int parking(int low, int high) { //在low~high间随机停的车的数量 if(high - low < L) return 0; else { srand(time(0)); int x = low + rand() % (high - L - low + 1); //在[low,high-L]区间随机取一个位置为车子‘前沿点’的位置 //cout << "x = " << x << endl; return parking(low, x) + 1 + parking(x + L, high); } } int main() { L = 2; cout << parking(0, 6); return 0; } // x = 4 // x = 1 // 2 // x = 2 // x = 0 // x = 4 // 3