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

1.编写递归函数int change(char*s),将字符串s中的所有空格后的字母转换为大写字母。注:不允许使用string.h头文件和其声明的相关函数。

cpp 复制代码
#include <stdio.h>

int change(char *str) {
	int i=0;
	while(str[i]!='\0') {
		if(str[i]==' '&&str[i+1]>-'a'&&str[i+1]<='z')
			str[i+1]=str[i+1]-'a'+'A';
		i++;
	}
	printf("%s",str);
}

2.编写函数,将一个正整数分解质因数,即多个质因子相乘的形式,只需返回质因子的个数。例: 90=2*3*3*5, 函数返回4

cpp 复制代码
#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 getcount(int n){
	int count=1;
	for(int i=2;i<n;i++)
	if(n%i==0&&isprime(i)){
		n/=i;	
		i--;
		count++;
	}
	return count;
}

int main(){
	printf("%d",getcount(90));
}

3.编写函数int de(int a[][10], int n),求二维整型数组中每行的和,并删除和最大的一行.要求:数组中剩余元素保存原来次序顺序存储,并返回最大的和。

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10

int del(int a[][N],int n) {
	int max=0,maxflag,sum;
	for(int i=0; i<n; i++) {
		sum=0;
		for(int j=0; j<n; j++)
			sum+=a[i][j];
		if(sum>max) {
			max=sum;
			maxflag=i;
		}
	}
	for(int i=maxflag; i<n; i++)
		for(int j=0; j<n; j++)
			a[i][j]=a[i+1][j];
	return max;
}

void generateRandomArray(int arr[][N], int rows, int cols) {
	for (int i = 0; i < rows; i++)
		for (int j = 0; j < cols; j++)
			arr[i][j] = rand() % 100;
}

void printArray(int arr[][N], int rows, int cols) {
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++)
			printf("%4d ", arr[i][j]);
		printf("\n");
	}
}

int main() {
	int arr[N][N];
	srand(time(NULL));
	generateRandomArray(arr, N, N);
	printf("max = %d\n",del(arr,N));
	printArray(arr, N-1, N);
	return 0;
}

4.学生成绩信息包含学号、姓名和成绩三项,定义存储上述学生成绩信息的单向链表的结点类型,并编写函数,由键盘输入n个学生的成绩信息,创建一个用于管理学生成绩信息的单向链表A,并在创建过程中随时保证单向链表的结点顺序满足成绩从低到高。

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

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

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

5.编写函数,从文件classB.txt中读取另一个班级的学生成绩信息创建链表B (文件classB.txt 中的信息按照成绩从低到高的顺序存储),将单向链表B与上题中的单向链表A归并为一个按学生成绩从低到高排序的单向链表。

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

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

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

struct node *creatfromfile() {
	FILE *file;
	if((file=fopen("classB.txt","r"))==NULL) {
		printf("open error");
		exit(0);
	}
	struct node *head=(struct node *)malloc(sizeof(struct node));
	struct node *pre=head;
	while(!feof(file)) {
		struct node *p=(struct node *)malloc(sizeof(struct node));
		fscanf(file,"%d %s %d",&p->num,&p->name,&p->score);
		while(pre->next!=NULL&&pre->next->score<p->score)
			pre=pre->next;
		p->next=pre->next;
		pre->next=p;
		pre=head;
	}
	fclose(file);
	return head->next;
}

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(p->score<q->score) {
			rear->next=p;
			rear=p;
			p=p->next;
		} else {
			rear->next=q;
			rear=q;
			q=q->next;
		}
	}
	if(p!=NULL)
		rear->next=p;
	else
		rear->next=q;
	return head->next;
}
相关推荐
卡尔曼的BD SLAMer18 分钟前
计算机视觉与深度学习 | Python实现EMD-SSA-VMD-LSTM时间序列预测(完整源码和数据)
python·深度学习·算法·cnn·lstm
yu_anan11125 分钟前
Denoising Score Matching with Langevin Dynamics
算法·机器学习·概率论
小葡萄20251 小时前
黑马程序员C++2024新版笔记 第三章 数组
笔记·算法·c++20
勇闯逆流河8 小时前
【数据结构】堆
c语言·数据结构·算法
pystraf8 小时前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
飞川撸码9 小时前
【LeetCode 热题100】739:每日温度(详细解析)(Go语言版)
算法·leetcode·golang
yuhao__z10 小时前
代码随想录算法训练营第六十六天| 图论11—卡码网97. 小明逛公园,127. 骑士的攻击
算法
Echo``10 小时前
3:OpenCV—视频播放
图像处理·人工智能·opencv·算法·机器学习·视觉检测·音视频
Nobkins10 小时前
2021ICPC四川省赛个人补题ABDHKLM
开发语言·数据结构·c++·算法·图论
88号技师10 小时前
2025年6月一区SCI-不实野燕麦优化算法Animated Oat Optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法