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

1.编写函数,打印下列序列0,1,1,2,3,5,8,13,21,34...(斐波那契序列)的前n项

#include <stdio.h>

int main() {
	int x=0,y=1,z,n;
	scanf("%d",&n);
	if(n==1)
		printf("%d\n",x);
	if(n==2)
		printf("%d\n%d\n",x,y);
	if(n>2) {
		printf("%d\n%d\n",x,y);
		for(int i=2; i<n; i++) {
			z=x+y;
			x=y;
			y=z;
			printf("%d\n",z);
		}
	}
}

2.编写程序打印下列三角矩阵。要求:下三角矩阵的各行值由名为yanghui的函数来计算完成;打印的行数在主函数中由键盘输入;打印在主函数进行。

1 1

1 2 1

1 3 3 1

1 4 6 4 1 

1 5 10 10 5 1

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

void show(int **arr,int n) {
	for(int i=0; i<n; i++) {
		for(int j=0; j<=i; j++)
			printf("%3d ",arr[i][j]);
		printf("\n");
	}
}

int main() {
	int n;
	scanf("%d",&n);
	int **arr=(int **)malloc(n*sizeof(int*));
	for(int i=0; i<n; i++)
		arr[i]=(int *)malloc(n*sizeof(int));
	for(int i = 0; i < n; i++)
		for(int j = 0; j <= i; j++)
			if(j == 0 || j == i)
				arr[i][j] = 1;
			else
				arr[i][j] = arr[i - 1][j - 1]+ arr[i - 1][j];
	show(arr,n);
}

3.编写函数,实现两个字符串变量值的交换。并编写主函数,在其中输入两个字符串,然后调用该函数,最后打印输出交换后的两个字符串变量值。要求:形式参数是指针(最多不超过100个字符)注:不允许用strcpy函数直接拷贝实现,两个字符串长度不一定相等

#include <stdio.h>

void swapStrings(char** str1, char** str2) {
	char* temp = *str1;
	*str1 = *str2;
	*str2 = temp;
}

int main() {
	char str1[100], str2[100];
	scanf("%s",&str1);
	scanf("%s",&str2);
	swapStrings(&str1, &str2);
	printf("%s\n", str1);
	printf("%s\n", str2);
	return 0;
}

4.从键盘读入10名评委的评分。扣除一个最高分及一个最低分,然后统计总分,并除以8,最后得到这个选手的最后得分(打分采用百分制)并输出之

#include <stdio.h>

int main(){
	int n=10,max=0,min=999;
	int score,sum=0;
	while(n!=0){
		scanf("%d",&score);
		if(score>max)
			max=score;
		if(score<min)
			min=score;
		sum+=score;
		n--;
	}
	sum=sum-max-min;
	printf("%lf%%",1.0*sum/8*100);
}

5.编写函数(函数名为CreatList),建立一个通讯录链表。该通讯录的信息包括姓名和手机号码。在主函数中读取该链表,并将链表的信息写入文件address.txt中

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

typedef struct node{
	char name[20];
	int num;
	struct node *next;
}node;

struct node *CreatList(int n){
	struct node *head=(struct node*)malloc(sizeof(struct node));
	head->next=NULL;
	struct node *pre=head;
	int i;
	for(i=0;i<n;i++){
		struct node *p=(struct node*)malloc(sizeof(struct node));
		scanf("%s %d",&p->name,&p->num);
		p->next=pre->next;
		pre->next=p;
	}
	return head->next;
}

void writelist(struct node *head){
	FILE *file;
	if((file=fopen("address.txt","w"))==NULL){
		printf("open error");
		exit(0);
	}
	struct node *p=head;
	while(p!=NULL){
		fprintf(file,"%s %d",&p->name,&p->num);
		p=p->next;
	}
	fclose(file);
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值