高级语言讲义2023软专(仅高级语言部分)

1.辗转相除求最大公约数过程如下:

U/V...余r_{0}

V/r_{0}...余r_{1}

r_{n-1}/r_{n}...余r_{n+1}

r_{n+1}为0时,r_{n}即为U、V最大公约数,编写函数int g< d(intU,intV)求最大公约数。

#include <stdio.h>

int gcd(int a,int b) {
	if(b==0)
		return a;
	else
		return gcd(b,a%b);
}

int gcd2(int a,int b) {
	int temp;
	if(a<b) {
		temp=a;
		a=b;
		b=temp;
	}
	while(b!=0) {
		temp=a%b;
		a=b;
		b=temp;
	}
	return a;
}

2.已知合法十六进制非负整数是从0.或ox开始,由数字0~9,小写字母a~f, 大写字母A~F构成的连续字符串,从键盘输入以'@'结束的任意长度字符串(可能含空格,制表,回车换行等字符),编写程序统计其中出现的合法十六进制非负整数的个数,找出其中最大的并输出。例如如下输入字符串中有5个16进制整数为: 0x564, 0x4879,0x00798, 0xfff12121 和0xabc12121,其中最大为oxffff12121

0x564   0x4897 erty 0x00798

0xffff12121 + + %0xabc12121 @

注:输入字符串长度不限,其中出现的所有合法十六进制数都在int范围,不考虑类型溢出问题。

#include <stdio.h>

int strlen(char *str) {
	int i=0;
	while(str[i]!='\0')
		i++;
	return i;
}

void strcpy(char *str1,char *str2) {
	int i=0;
	while(str1[i]!='\0') {
		str2[i]=str1[i];
		i++;
	}
	str2[i]-'\0';
}

int strcmp(char *str1,char *str2) {
	int l1=strlen(str1);
	int l2=strlen(str2);
	if(l1>l2)
		return 1;
	else if(l2>l1)
		return -1;
	int i=0;
	while(str1[i]!='\0') {
		if(str1[i]>str2[i])
			return 1;
		else if(str1[i]<str2[i])
			return -1;
		else
			i++;
	}
	if(str1[i]=='\0'&&str2[i]=='\0')
		return 0;
}

int islegal(char ch) {
	if(ch>='a'&&ch<='z')
		return 1;
	else if(ch>='0'&&ch<='9')
		return 1;
	return 0;
}

int main() {
	char str[100],temp[100],maxnum[100];
	scanf("%s",str);
	int len=0,num=0,i=0;
	while(str[i]!='\0')
		if(str[i]=='0'&&(str[i+1]=='x'||str[i+1]=='X')) {
			temp[0]=str[i];
			temp[1]=str[i+1];
			i+=2;
			len=2;
			while(islegal(str[i])) {
				temp[len++]=str[i];
				i++;
			}
			temp[len]='\0';
			num++;
			if(strcmp(temp,maxnum)==1)
				strcpy(temp,maxnum);
			temp[0]='\0';
		} else
			i++;
	printf("%d %s",num,maxnum);
}

3. (3x+5y)的n次幂的二项式展开有项,按照x的指数升序排列,编写递归函数,返回该展开式的第k(0<=k<=n)项的系数,递归函数声明参考形式为int find (int n, int k).

#include <stdio.h>

int find(int n,int k) {
	if(n=k&&k==0)
		return 1;
	if(n==k)
		return 3*find(n-1,k-1);
	if(k==0)
		return 5*find(n-1,k);
	return 3*find(n-1,k-1)+5*find(n-1,k);
}

4.假设会员卡信息包含昵称和手机号,全部使用字符串储存,其中手机号是会员唯一标识。现有两个单项链表存储会员卡(其上信息完全不同),已按手机号递增排序,请自定义链表结点类型,并编写函数,通过改变链表中各结点连续关系 将两个单向链表合并成一个递增链表)排序后得新的表头作为函数返回值。

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

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

int strcmp(char *str1,char *str2) {
	int i=0;
	while(str1[i]!='\0'&&str2[i]!='\0') {
		if(str1[i]>str2[i])
			return 1;
		else if(str1[i]>str2[i])
			return -1;
		else
			i++;
	}
	if(str1[i]=='\0'&&str2[i]=='\0')
		return 0;
	else if(str1[i]=='\0')
		return -1;
	else
		return 1;
}

struct node *merge(struct node *head1,struct node *head2) {
	struct node *head=(struct node *)malloc(sizeof(struct node));
	struct node *p=head1,*q=head2,*rear=head;
	while(p!=NULL&&q!=NULL) {
		if(strcmp(p->num,q->num)) {
			rear->next=p;
			rear=p;
			p=p->next;
		} else {
			rear->next=q;
			rear=q;
			q=q->next;
		}
	}
	if(p==NULL)
		rear->next=q;
	else
		rear->next=q;
	return head->next;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值