高级语言期末2016级唐班B卷(软件学院)

1.编写函数,将两个非递减整形数组a和b合并后存储在a中,并保证处理后的数组仍然按照非递减顺序存储,函数返回值为出现次数最多的元素值,不能定义额外的新数组。
函数声明为:int merge(int *a ,int m, int *b, int n)

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

int merge(int *a,int m,int *b,int n) {
	for(int i=m; i<m+n; i++)
		a[i]=b[i-m];
	sort(a,m+n);
	for(int j=0; j<n+m; j++)
		printf("%d ",a[j]);
    int flag = a[0];
    int maxCount = 1;
    for (int i = 0; i < m + n; i++) {
        int count = 1;
        for (int j = i + 1; j < m + n; j++) {
            if (a[i] == a[j])
                count++;
            else
                break;
        }
        if (count > maxCount) {
            maxCount = count;
            flag = a[i];
        }
    }
    return flag;
}

int main() {
	int a[]= {1,1,4,4,4,4};
	int b[]= {2,2,2,8};
	printf("\n%d",merge(a,6,b,4));
}

2.编写函数,求一个正整数的最小质因数

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

bool Isprime(int n) {
	if(n<=1)
		return false;
	for(int i=2; i<=sqrt(n); i++) {
		if(n%i==0)
			return false;
	}
	return true;
}

int find(int n) {
	for(int i=2; i<=n; i++)
		if(n%i==0&&Isprime(i))
			return i;
}

3.一个长度为n(n>5),可以在其2/5处折断,变为两段长度分别为2n/5,3n/5的木条;如果得到的木条的长度仍大于五,则继续按照上述方法折断,直到任意木条的长度均不大于5为止。编写递归函数,计算一个长度为n的木条,最后会被折断为多少根木条。

#include <stdio.h>

int cut(int n) {
	if(n<=5)
		return 1;
	else return cut(n*2/5)+cut(n*3/5);
}

4.CCF会员的信息卡至少包括:会员号(5位数字+1位大写字母)、姓名、会员类别、有效期(包含年、月、日)等信息
例如:15316S、吕帅、高级会员、20181231
1)定义存储CCF会员上述信息的单向链表结点类型;
2)编写函数,依次由文件in.txt读入所有会员的信息(文件中每个会员信息包括会员号、姓名、有效期);会员类别为冗余信息,由会员号的最后一位可以推导出,创建一个用于管理会员信息的单向链表,使该链表有序;
3)编写函数,将上述链表逆序。

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

typedef struct user {
	int num;
	char kind;
	char name[20];
	int date;
	struct user *next;
} user;

void creat() {
	FILE *file;
	file=fopen("in.txt","r");
	struct user *p;
	struct user *head;
	while(!feof(file)) {
		p=(struct user*)malloc(sizeof(struct user));
		fscanf(file,"%d%c%s%d",&p->num,&p->kind,p->name,&p->date);
		p->next=NULL;
		if(head==NULL)
			head=p;
		else {
			struct user *p1=head,*q=NULL;
			if(p1->num>p->num) {
				p->next=head;
				head=p;
			}
			while(p1->num<p->num&&p1!=NULL) {
				q=p;
				p1=p1->next;
			}
			if(p1==NULL)
				q->next=p;
			else {
				q->next=p;
				p->next=p1;
			}
		}
	}
	fclose(file);
	return;
}

struct user * reverse(struct user *head) {
	struct user *dummyhead=(struct user*)malloc(sizeof(struct user));
	dummyhead->next=NULL;
	while(head!=NULL) {
		struct user *temp=head->next;
		head->next=dummyhead->next;
		dummyhead->next=head;
		head=temp;
	}
	return dummyhead->next;
}
  • 52
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值