思路
- 权重模拟
typedef struct {
int max;
int count;
int w[10001];
} Solution;
Solution* solutionCreate(int* w, int wSize) {
Solution* t = (Solution*)malloc(sizeof(Solution));
t->max =0;
t->count = wSize;
int i;
for(i=0;i<wSize;i++){
t->w[i] = w[i];
t->max += w[i];
}
return t;
}
int solutionPickIndex(Solution* obj) {
int t =rand()%(obj->max)+1;
int i;
for(i=0;i<obj->count;i++){
if(t>obj->w[i])
t-=obj->w[i];//相减,以长度关系模拟区间
else
break;
}
return i;
}
void solutionFree(Solution* obj) {
free(obj);
}
/**
* Your Solution struct will be instantiated and called as such:
* Solution* obj = solutionCreate(w, wSize);
* int param_1 = solutionPickIndex(obj);
* solutionFree(obj);
*/