高级语言期末2004级A卷(计算机学院)

1.编程序,打印前十对孪生素数。若两个素数之差为2,则称为孪生素数,例如(3,5)(5,7)(11,13)

#include <stdio.h>
#include <math.h>

int isprime(int n) {
	if(n<=1)
		return 0;
	for(int i=2; i<=sqrt(n); i++) {
		if(n%i==0)
			return 0;
	}
	return 1;
}

int main() {
	int count=0;
	for(int i=3; count<10; i++)
		if(isprime(i)&&isprime(i+2)) {
			printf("%d %d\n",i,i+2);
			count++;
		}
}

2.设幂数为非负整数的多项式。将每一项的系数和幂次存于下表:

#include <stdio.h>

typedef struct muti {
	int pow;
	float coe;
} muti;

float getresult(struct muti p[],int x) {
	float result=0,temp;
	int i=0;
	while(p[i].pow>0) {
		temp=p[i].coe;
		for(int j=0; j<p[i].pow; j++)
			temp*=x;
		result+=temp;
		i++;
	}
	if(p[i].pow==0)
		result+=p[i].coe;
	return result;
}

3.编程序判断10阶整数方阵是否关于主对角线对称

#include <stdio.h>

int issym(int maxt[][10]) {
	for(int i=0; i<10; i++)
		for(int j=0; j<10; j++)
			if(maxt[i][j]!=maxt[j][i])
				return 0;
	return 1;
}

4.编写一个函数,用递归计算Hermite多项式的函数,Hermite多项式定义为

#include <stdio.h>

int func(int n,int x) {
	if(n==0)
		return 1;
	else if(n==1)
		return 2*x;
	return 2*x*func(n-1,x)-2*(n-1)*func(n-2,x);
}

5.编函数,用冒泡实现由整数组成的单向链表的排序

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
	int key;
	struct node *next;
} node;

int listlen(struct node *head) {
	int count=0;
	while(head!=NULL) {
		count++;
		head=head->next;
	}
	return count;
}

struct node *sort(struct node *head) {
	if(head==NULL||head->next==NULL)
		return head;
	int n=listlen(head);
	struct node *dummyhead=(struct node*)malloc(sizeof(struct node));
	dummyhead->next=head;
	for(int i=0; i<n-1; i++) {
		int swap=0;
		struct node *p=dummyhead->next,*pre=dummyhead;
		for(int j=0; j<n-i-1; j++) {
			if(p->key>p->next->key) {
				struct node *q=p->next;
				pre->next=q;
				pre=q;
				p->next=q->next;
				q->next=p;
				swap=1;
			} else {
				pre=p;
				p=p->next;
			}
		}
		if(swap==0);
		break;
	}
	return dummyhead->next;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值