吉林大学2018《程序设计基础》(2)

1.递归函数实现有序的整形数组进行二分检索

在这里插入图片描述
下面展示一些 内联代码片

#include<stdio.h>
//递归函数实现有序的整形数组进行二分检索
int binarysearch(int a[],int n,int key){
    if(n==1)return (key==a[0]);
    int mid=n/2;
    if(a[mid]==key)return 1;
        else if(a[mid]>key)return binarysearch(a,mid,key);
        else return binarysearch(a+mid+1,n-1-mid,key);
}
int main(){
    int n;
    int m;
    scanf("%d",&n);
    int a[n];
    int i=0;
    for(i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    m=binarysearch(a,n,3);
    printf("%d",m);
}

思考:在编写的时候第一编忽略了n==1的情况在不相等的最后算作一个程序出口
踩分点:递归函数;左半部分,右半部分

2.学生成绩单管理

在这里插入图片描述
下面展示一些 内联代码片

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student {
    int number;
    char name[20];
    int dis_score;
    int report_score;
    int test_score;
    struct Student* next;
};
struct Student* create_list() {
    struct Student* head, * p, * tail;
    head = (struct Student*)malloc(sizeof(struct Student));
    tail = head;
    tail->next = NULL;
    FILE* f;
    if ((f = fopen("2018Exp.txt", "r")) == NULL) {
        printf("Error opening file!\n");
        exit(1);
    }
    while (!feof(f)) {
        p = (struct Student*)malloc(sizeof(struct Student));
        fscanf(f, "%d", &p->number);
        fscanf(f, "%s", p->name);
        fscanf(f, "%d", &p->dis_score);
        fscanf(f, "%d", &p->report_score);
        fscanf(f, "%d", &p->test_score);
        p->next = NULL;
        tail->next = p;
        tail = p;
    }
    fclose(f);
    return head;
}
void print_list(struct Student* head) {
    struct Student* p = head->next; // 跳过哨兵节点
    while (p != NULL) {
        printf("Number: %d\n", p->number);
        printf("Name: %s\n", p->name);
        printf("Dis score: %d\n", p->dis_score);
        printf("Report score: %d\n", p->report_score);
        printf("Test score: %d\n", p->test_score);
        printf("\n");
        p = p->next; // 指向下一个节点
    }
}
void free_list(struct Student* head) {
    struct Student* p = head, * q;
    while (p != NULL) {
        q = p->next;
        free(p);
        p = q;
    }
}
int main() {
    struct Student* head = create_list();
    print_list(head);
    free_list(head);
    return 0;
}

3.构建单向链表按照成绩从高到低排序

在这里插入图片描述
在这里插入图片描述
下面展示一些 内联代码片

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student {
    int number;
    char name[20];
    int dis_score;
    int report_score;
    int test_score;
    struct Student* next;
};
struct Student* create_list() {
    struct Student* head, * p, * tail;
    head = (struct Student*)malloc(sizeof(struct Student));
    tail = head;
    tail->next = NULL;
    FILE* f;
    if ((f = fopen("2018Exp.txt", "r")) == NULL) {
        printf("Error opening file!\n");
        exit(1);
    }
    while (!feof(f)) {
        p = (struct Student*)malloc(sizeof(struct Student));
        fscanf(f, "%d", &p->number);
        fscanf(f, "%s", p->name);
        fscanf(f, "%d", &p->dis_score);
        fscanf(f, "%d", &p->report_score);
        fscanf(f, "%d", &p->test_score);
        p->next = NULL;
        tail->next = p;
        tail = p;
    }
    fclose(f);
    return head;
}
void print_list(struct Student* head) {
    struct Student* p = head->next; // 跳过哨兵节点
    while (p != NULL) {
        printf("Number: %d\n", p->number);
        printf("Name: %s\n", p->name);
        printf("Dis score: %d\n", p->dis_score);
        printf("Report score: %d\n", p->report_score);
        printf("Test score: %d\n", p->test_score);
        printf("total score:%.f",(p->dis_score*0.2+p->report_score*0.2+p->test_score*0.6));
        printf("\n");
        p = p->next; // 指向下一个节点
    }
}
void free_list(struct Student* head) {
    struct Student* p = head, * q;
    while (p != NULL) {
        q = p->next;
        free(p);
        p = q;
    }
}
void sort(struct Student*head){//带有哨兵节点的链表排序
    struct Student *p,*p0,*r,*r0,*q;
    int pscore,rscore;
    p0=head;
    p=head->next;
    while(p!=NULL){
        r0=head;
        r=head->next;
        pscore=p->dis_score*0.2+p->report_score*0.2+p->test_score*0.6;
        rscore=r->dis_score*0.2+r->report_score*0.2+r->test_score*0.6;
        while ((rscore>pscore)&&(r!=p))
        {
            r0=r;
            r=r->next;
            rscore=r->dis_score*0.2+r->report_score*0.2+r->test_score*0.6;
        }
        if(r!=p){
            q=p;
            p0->next=p->next;
            p=p0;
            q->next=r;
            r0->next=q;
        }
        p0=p;
        p=p->next;
    }

}
int main() {
    struct Student* head = create_list();
    print_list(head);
    sort(head);
    printf("-----------\n");
    print_list(head);
    free_list(head);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值