我有一个难题。从概念上讲,我想我知道自己需要做什么。代码方面,我不太确定。
我想通过AVAIL_NURSE_W1[]
数组(其中包含与Week_1中哪个护士相对应的数字),生成一个随机数来决定该数组中的一个插槽(如果该插槽保持为零)值,然后生成另一个随机数并再试一次),取这个数字(这是一名护士),并将其放入MONDAY[]
数组。
相关代码:
int main() {
int randomNurse();
srand(time_t(NULL));
/*0 = Denise, 1 = Inja, 2 = Jane, 3 = Karen, 4 = Maggie, 5 = Margaret, 6 = MJ, 7 = Queen, 8 = Sherri*/
/*-----WEEKLY_ASSIGNMENT-----*/
int AVAIL_NURSE_W1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //holds the numerical values of the nurses that CAN work each week
/*scans in first week*/
for (int column = 0; column < 4; column++) {
for (int num = 0; num < 9; num++) {
if (AVAIL_NURSE_W1[num] == select[0][column])
AVAIL_NURSE_W1[num] = 0;
}
}
/*-----MONDAY-----*/
int MONDAY[5]; //days of the week, number of jobs on a given day
for (int e = 0; e < 5; e++) { //loop for positions on monday
while (MONDAY[e] == 0) {
int temp_assign = randomNurse();
if (AVAIL_NURSE_W1[temp_assign] != 0) { //if the nurse IS available...
MONDAY[e] = AVAIL_NURSE_W1[temp_assign];
AVAIL_NURSE_W1[temp_assign] = 0; //we don't want to repeat nurses
} else {
continue;
}
}
}
return 0;
}
/*function to generate random nurse*/
int randomNurse() {
return rand() % 9; //random number 0-8, to pick nurse
}
我的问题:
我如何处理从AVAIL_NURSE_W1[]
数组中获取可用护士,生成一个随机数,该数字决定从哪个槽中获取值,获取该值,将其存储在新数组MONDAY[]
中;如果AVAIL_NURSE_W1[]
数组中的值为零,则重复上述过程,直到选择非零值为止;选择一个值后,我会将所选值更改为零,然后再次循环。
期望的结果
MONDAY[]
数组应包含五个非零整数。没有重复。
到目前为止,while
循环条件似乎永远不会改变。
如果还有其他需要说的话,请告诉我。我希望我已经提供了足够的信息。
答案 0 :(得分:1)
你走了:
#include<stdio.h>
#include <stdlib.h>
const char names[9][10] = {"Denise", "Inja", "Jane", "Karen", "Maggie", "Margaret", "MJ", "Queen", "Sherri"};
const char days[5][10] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
int randomNurse();
int main() {
srand(time(NULL));
int day, e, pos, candidate;
int i,j;
/*0 = Denise, 1 = Inja, 2 = Jane, 3 = Karen, 4 = Maggie, 5 = Margaret, 6 = MJ, 7 = Queen, 8 = Sherri*/
/*-----WEEKLY_ASSIGNMENT-----*/
int AVAIL_NURSE_W1[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; //holds the status of each nurse, 1:available 0:unavailable
int pos_per_day[5] = {2, 5, 7, 4, 3}; // number of needed nurses per day, Monday:2 nurses, tuesday: 5 nurses ...
int select[5][9]; // the selected nurses per day
for(i=0; i<5; i++)
for(j=0; j<9;j++) select[i][j] = -1; // initialize to -1 which means no nurse is selected
// fill all the days of week
for (day = 0; day<5; day++) // for every day
{
for(pos = 0; pos<pos_per_day[day]; pos++ ) // for every position needed that day
{
do
{
candidate = randomNurse();
}while(!AVAIL_NURSE_W1[candidate]); // look for available nurse
AVAIL_NURSE_W1[candidate] = 0; // change her status to not available
select[day][pos] = candidate; // fill the output array with appropriate nurse
}
for(i=0; i< 9; i++)
{
AVAIL_NURSE_W1[i] = 1; // initialize the nurses status for next day use
}
}
for(i=0; i<5; i++) // display
{
printf("%-10s: ", days[i]);
for(j=0; j<9;j++)
{
if(select[i][j] != -1) printf("%-10s ", names[select[i][j]]);
}
printf("\n");
}
return 0;
}
/*function to generate random nurse*/
int randomNurse() {
return rand() % 9; //random number 0-8, to pick nurse
}