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

1.一个二维数组,每一行存放一个字符串,比较字符串并排序。

#include <stdio.h>
 
int strcmp(char *str,char *flag) {
	int i=0;
	while(str[i]!='\0'&&flag[i]!='\0') {
		if(str[i]>flag[i])
			return 1;
		else if(str[i]<flag[i])
			return -1;
		else
			i++;
	}
	if(str[i]=='\0'&&flag[i]=='\0')
		return 0;
	else if(str[i]=='\0')
		return -1;
	else
		return 1;
}
 
void strcpy(char *flag,char *str) {
	int i=0;
	while(str[i]!='\0') {
		flag[i]=str[i];
		i++;
	}
	flag[i]='\0';
}
 
void sort(char st[][10],int n) {
	char temp[10];
	for(int i=0; i<n-1; i++)
		for(int j=0; j<n-i-1; j++)
			if(strcmp(st[j],st[j+1])>0) {
				strcpy(temp,st[j]);
				strcpy(st[j],st[j+1]);
				strcpy(st[j+1],temp);
			}
}
 
int main() {
	char st[][10] = {"hello","world","python","C","java","R"};
	int n=6;
	sort(st,n);
	for(int i=0; i<n; i++)
		printf("%s ",st[i]);
	printf("\n");
	return 0;
}

2.一行n(n大于等于2)个框,三个颜色,每个相邻的框颜色不能相同,用递归写出有多少种方法

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

bool islegal(int *path,int n) {
	for(int i=0; i<n; i++)
		if(path[i]==path[i-1]||path[i]==path[i+1])
			return false;
	return true;
}

void dfs(int *path,int n,int index,int *num) {
	if(index==n) {
		if(islegal(path,n))
			*num++;
		return;
	}
	for(int i=0; i<=2; i++) {
		path[index]=i;
		dfs(path,n,index+1,num);
	}
}

int count(int n) {
	int path[20];
	int num=0;
	dfs(path,n,0,&num);
	return num;
}

3.定义结构体,链表输入学生数据,再输入到文件中。

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

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

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

4.将该链表按性别拆分为两个链表并排序,再写到两个文件中

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

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

void save(struct stu *head) {
	FILE *fileA,*fileB;
	if((fileA=fopen("man.txt","w"))==NULL) {
		printf("open error");
		exit(0);
	}
	if((fileB=fopen("woman.txt","w"))==NULL) {
		printf("open error");
		exit(0);
	}
	struct stu *p=head;
	struct stu *headA=(struct stu *)malloc(sizeof(struct stu));
	struct stu *headB=(struct stu *)malloc(sizeof(struct stu));
	struct stu *rearA=headA,*rearB=headB;
	while(p!=NULL) {
		struct stu *flag=p->next;
		if(p->sex==1) {
			rearA->next=p;
			rearA=p;
			fprintf(fileA,"%d %s",p->num,p->name);
			fprintf(fileA,"man");
		} else {
			rearB->next=p;
			rearB=p;
			fprintf(fileB,"%d %s",p->num,p->name);
			fprintf(fileB,"woman");
		}
		p=flag;
	}
	rearA->next=NULL;
	rearB->next=NULL;
	fclose(fileA);
	fclose(fileB);

}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值