高级语言期末2013级A卷(软件学院)

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.编写函数,实现按照如下公式计算的功能

f(n)=2/(3*0!)+3/(4*1!)+4/(5*2!)+...+n/((n+1)*(n-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多项式的函数,公式递归定义如下

h_{n}(x)=\left\{\begin{matrix} 1,n=0 & & \\ 2x,n=1& & \\ 2xh_{n-1}(x)-2(n-1)h_{n-2}(x),n>1 & & \end{matrix}\right.

#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;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值