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;
}