当前位置:编程学习 > C/C++ >>

求一个C语言程序。很急。谢谢!积分多多。

模拟高考平行志愿投档

要求:

1.在输入每个考生的成绩、志愿,学校的招生计划情况下能完成模拟平行志愿方式的高考志愿投档,最后输出每个学生的投档学校。如果考生不能投到某个学校,则输出“NO”

说明:

1.成绩总分由语文、数学、英语和综合四门课程组成,成绩排名先按总分,再按语文、数学、英语和综合进行。

2.学生志愿可选择3个学校,如果学校名称输错则该考生无法投档。

3.平行志愿投档规则为:平行志愿按分数优先原则投档。即首先将同一科类批次线上的“自由可投”的考生按投档成绩从高分到低分排序投档,当遇到多名考生特征成绩相同时,依次按语、数、外、综合科目的成绩从高分到低分排序投档;对单个考生投档时,根据考生填报的平行志愿中的院校志愿顺序,投档到排序相对在前且有计划余额的院校。

 

答案:这个代码你可以参考下,跟你要的不完全一样~~~话说这是作业吧……

 

#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
        

上一个:2个数的最大公约数和最小公倍数 C语言怎么求?
下一个:用C语言求任意两个矩阵的乘积的子程序

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,