c语言实训题目设计报告 模拟高考平行志愿投档,(求)C语言:模拟高考平行志愿投档...

该博客实现了一个基于优先级的应聘者分配算法。首先,根据输入的职位需求和应聘者评分,创建一个有序的应聘者列表。然后,通过一个队列结构,将应聘者按评分高低依次雇佣到相应的职位,若应聘者的第一志愿已满,则考虑其第二志愿。最后,输出被雇佣的应聘者及其职位和未被雇佣的应聘者信息。
摘要由CSDN通过智能技术生成

#include

#include

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值