这个貌似你可以参考一下。你需要的程序跟这个不是完全一样。这个仅供参考
程序代码:
#include <stdio.h>
#include <malloc.h>
typedef struct struct_node{
int number;
int score;
int application[3];
struct struct_node * next;
} node;
typedef struct {
node * first;
node * last;
} queue;
typedef struct {
node * head;
} list;
void init_queue(queue * q) {
q->first = q->last = NULL;
}
void enqueue(queue * q, node * n) {
if (q->first == NULL) {
q->first = q->last = n;
} else {
q->last->next = n;
q->last = q->last->next;
}
}
node * dequeue(queue * q) {
node * n = q->first;
if (q->first == q->last) {
q->first = q->last = NULL;
} else {
q->first = q->first->next;
}
return n;
}
int init_list(list * l) {
l->head = (node *) malloc(sizeof(node));
if (l->head) {
l->head->next = NULL;
return 0;
} else {
return 1;
}
}
void insert_list_element(list * l, node * n) {
node * q = l->head, * p = q->next;
while (p && p->score > n->score) {
q = p;
p = p->next;
}
q->next = n;
n->next = p;
}
void remove_list_element(list * l, node * n) {
node * p = l->head;
if (n == NULL) {
return;
}
while (p && p->next != n) {
p = p->next;
}
if (p) {
p->next = p->next->next;
}
}
int main() {
int m, * need, n, i;
list applicants;
node * applicant, * p;
queue * position;
printf("\nHow many positions available are there? ");
fflush(stdout);
scanf("%d", &m);
need = (int *) malloc(sizeof(int) * m);
if (need == NULL) {
printf("\nFailed to allocate memory.\nApplication quited.\n");
fflush(stdout);
return 1;
}
printf("\nHow many people are needed for every position?\n");
printf("Flow the order position 1, position 2 ...\n");
fflush(stdout);
for (i = 0; i < m; i++) {
scanf(" %d", &need[i]);
}
printf("\nHow many applicants are there? ");
fflush(stdout);
scanf("%d", &n);
if (init_list(&applicants)) {
printf("\nFailed to allocate memory.\nApplication quited.\n");
fflush(stdout);
return 1;
}
printf("\nTell me about the applicants in this format:\n");
printf("score application_1 application_2\n");
printf("Following the order applicant 1, applicant 2 ...\n");
fflush(stdout);
for (i = 0; i < n; i++) {
applicant = (node *) malloc(sizeof(node));
applicant->number = i;
scanf(" %d %d %d", &applicant->score,
&applicant->application[1], &applicant->application[2]);
applicant->application[0] = applicant->application[1];
insert_list_element(&applicants, applicant);
}
position = (queue *) malloc(sizeof(queue) * m);
if (position == NULL) {
printf("\nFailed to allocate memory.\nApplication quited.\n");
return 1;
}
for (i = 0; i < m; i++) {
init_queue(&position[i]);
}
// Employ the applicants according to their scores.
// Let p point at the head of list applicants. Couse the list applicants is sorted descendingly,
// at beginning, p->next points at the applicant with the best score.
for (p = applicants.head, applicant = p->next; applicant; applicant = p->next) {
// If the position for which the applicant applied is available.
if (need[applicant->application[0]]) {
// Employe the applicant to the position he/she applied for.
enqueue(&position[applicant->application[0]], applicant);
// Couse the applicant is employed already, he/she should get out of the list of applicants.
remove_list_element(&applicants, applicant);
// Couse we employd 1 person for the position, the need of the position should be decreased by 1.
need[applicant->application[0]]--;
// If the position for which the applicant applied is NOT available.
} else {
// If the application is the applicant's second one, he/she can not be employed
if (applicant->application[0] == applicant->application[2]) {
// Couse his/her score was subtracted by 5 when we try to employ him/her according to his/her second
// application, now we need to restore his/her score to original state.
applicant->score += 5;
// Let p point at the applicant. That means, from now on, wo employ only the people behind him/her,
// all the people and him/her will never be consider any more.
p = applicant;
// If the application is the applicant's first one, he/she still has chance to be employed
} else {
// We subtract the applicant's score by 5.
applicant->score -= 5;
// Try to employ him/her according to his/her second appliction.
applicant->application[0] = applicant->application[2];
// By let him/her out of the list and comback again, he/she is arranged to the correct position
// in the according to his/her new score.
remove_list_element(&applicants, applicant);
insert_list_element(&applicants, applicant);
}
}
}
// Show the employed applicants.
for (i = 0; i < m; i++) {
printf("\nThe applicants employed for position %d are:\n", i);
while (position[i].first) {
applicant = dequeue(&position[i]);
printf("No.%d Score: %2d 1st application: %d 2nd application: %d\n",
applicant->number, applicant->score, applicant->application[1], applicant->application[2]);
fflush(stdout);
}
}
applicant = applicants.head->next;
if (applicant == NULL) {
printf("\nAll applicants are employed.\n");
} else {
printf("\nThese applicants are not employed:\n");
while (applicant) {
printf("No.%d Score: %d 1st application: %d 2nd application: %d\n",
applicant->number, applicant->score, applicant->application[1], applicant->application[2]);
fflush(stdout);
applicant = applicant->next;
}
}
}
[
本帖最后由 voidx 于 2011-6-24 14:56 编辑 ]