1.对长度为n的整型数组a进行冒泡排序
#include <stdio.h>
void sort(int *a,int n) {
for(int i=0; i<n-1; i++)
for(int j=0; j<n-i-1; j++)
if(a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
2.编写函数,实现按照如下公式计算的功能
#include <stdio.h>
int fac(int n) {
if(n==0)
return 1;
else
return n*fac(n-1);
}
int func(int n){
float flag;
float sum=0;
for(int i=2;i<=n;i++){
flag=i/((i+1)*fac(i-2));
sum+=flag;
}
return sum;
}
3.编写一个函数,按照如下定义计算Hermite多项式的函数,公式递归定义如下
#include <stdio.h>
int func(int n,int x) {
if(n==0)
return 1;
else if(n==1)
return 2*x;
else
return 2*x*func(n-1,x)-2*(n-1)*func(n-2,x);
}
4.定义存储学生信息的结构体至少应包含:学号、姓名、成绩、指向下一个结构体的指针等字段。编写函数,从指定文件class531316.txt中读入所有学生信息,(假定文件中存储信息与结构体信息格式对应),构建为图1所示的链表。
#include <stdio.h>
#include <stdlib.h>
typedef struct student {
int num;
char name[20];
int score;
struct student* next;
} student;
struct student* create() {
FILE *file;
if((file=fopen("class531316.txt","r"))==NULL) {
printf("open error");
exit(0);
}
struct student *head=(struct student*)malloc(sizeof(struct student));
head->next=NULL;
struct student *rear=head;
while(!feof(file)) {
struct student *p=(struct student*)malloc(sizeof(struct student));
fscanf(file,"%d %s %d",&p->num,&p->name,&p->score);
p->next=rear->next;
rear->next=p;
rear=p;
}
return head->next;
}
5.给定链表,每个结点包含:整数信息key和后继指针next。编写函数,删除该链表中的具有最大key值和最小key值得结点(注:key值可能重复)
例如,若链表中存储的key值依次为1、0、3、5、2、5、3、0、7、9、1,最大key值为9,最小key值为0,则处理后的链表中存储的key值为1、3、5、2、5、3、7、1。
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int key;
struct node *next;
} node;
struct node *del(struct node *head) {
struct node *dummyhead=(struct node *)malloc(sizeof(struct node));
dummyhead->next=head;
struct node *maxp=head,*minp=head;
struct node *p=head,*pre;
while(p!=NULL) {
if(p->key<minp->key)
minp=p;
if(p->key>maxp->key)
maxp=p;
p=p->next;
}
p=head;
pre=dummyhead;
while(p!=NULL) {
if(p->key==minp->key||p->key==maxp->key)
pre->next=p->next;
else
pre=p;
p=p->next;
}
return dummyhead->next;
}