| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 727 人关注过本帖
标题:(求)C语言:模拟高考平行志愿投档
只看楼主 加入收藏
秋凉
Rank: 1
来 自:浙江省杭州市萧山区
等 级:禁止发言
帖 子:5
专家分:0
注 册:2011-6-24
结帖率:0
收藏
已结贴  问题点数:10 回复次数:10 
(求)C语言:模拟高考平行志愿投档
提示: 作者被禁止或删除 内容自动屏蔽
搜索更多相关主题的帖子: 高考志愿 招生计划 平行志愿 C语言 英语 
2011-06-24 10:32
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:3 
帮你可以 要付出报酬的

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2011-06-24 11:21
秋凉
Rank: 1
来 自:浙江省杭州市萧山区
等 级:禁止发言
帖 子:5
专家分:0
注 册:2011-6-24
收藏
得分:0 
回复 2楼 vandychan
提示: 作者被禁止或删除 内容自动屏蔽
2011-06-24 11:53
Toomj
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:257
专家分:1826
注 册:2011-5-17
收藏
得分:3 
孩纸,作业贴斑竹灰非常生气的
2011-06-24 12:00
秋凉
Rank: 1
来 自:浙江省杭州市萧山区
等 级:禁止发言
帖 子:5
专家分:0
注 册:2011-6-24
收藏
得分:0 
回复 4楼 Toomj
提示: 作者被禁止或删除 内容自动屏蔽
2011-06-24 12:16
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:3 
回复 5楼 秋凉
重不重修是你自己的事,自己的事要自己做
2011-06-24 12:55
秋凉
Rank: 1
来 自:浙江省杭州市萧山区
等 级:禁止发言
帖 子:5
专家分:0
注 册:2011-6-24
收藏
得分:0 
回复 6楼 voidx
提示: 作者被禁止或删除 内容自动屏蔽
2011-06-24 14:46
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:0 
这个貌似你可以参考一下。你需要的程序跟这个不是完全一样。这个仅供参考

程序代码:
#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 编辑 ]
2011-06-24 14:53
秋凉
Rank: 1
来 自:浙江省杭州市萧山区
等 级:禁止发言
帖 子:5
专家分:0
注 册:2011-6-24
收藏
得分:0 
回复 8楼 voidx
提示: 作者被禁止或删除 内容自动屏蔽
2011-06-24 15:04
unique5954
Rank: 1
等 级:新手上路
帖 子:5
专家分:2
注 册:2011-5-18
收藏
得分:3 
回复 8楼 voidx
小屁孩,你不好好学习,还想求救啊??被俊哥发现了哦!!!!
2011-06-24 15:23
快速回复:(求)C语言:模拟高考平行志愿投档
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020001 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved