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

1.编写bool函数,判定给定的正整数n,M是否满足:M为n的质因数(能整除n的质数)

cpp 复制代码
#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;
}

bool find(int n,int M)
{
	if(n%M==0&&Isprime(M))
		return true;
	return false;
}

2.编写函数,对给定的整数数组进行排序,使得所有正整数均出现在负整数和零之前

cpp 复制代码
#include <stdio.h>
 
void sort(int *arr,int n) {
	for(int i=0; i<n-1; i++)
		for(int j=0; j<n-i-1; j++)
			if(arr[j]<=0) {
				int temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
	for(int i=0; i<n-1; i++)
		for(int j=0; j<n-i-1; j++)
			if(arr[j]<0) {
				int temp=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
			}
}
 
int main() {
	int arr[]= {-1,4,-3,0,2,1,-9,7};
	sort(arr,8);
	for(int i=0; i<8; i++) {
		printf("%d ",arr[i]);
	}
}

3.编写函数,实现按照如下公式计算的功能,其中n为自然数

cpp 复制代码
#include <stdio.h>
 
int fac(int n) {
	if(n==0)
		return 1;
	else
		return n*fac(n-1);
}
 
float fun(int n) {
	float flag;
	float sum=0;
	for(int i=0; i<=n; i++) {
		flag=i/((i+1)*fac(i+2));
		sum+=flag;
	}
	return sum;
}

4.构造一个表示教师的结构体(包含3个字段,姓名,性别,年龄),编写函数读入M个教师的信息,存入一个结构体中

|---------|----------|--------|---------|
| 张三 | 李四 | ...... | 赵九 |
| 男(true) | 女(false) | ...... | 男(true) |
| 50 | 37 | ...... | 09 |

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

struct teacher{
	char name[10];
	bool sex;
	int age;
};

void save(struct teacher st[], int M)
{
	for(int i = 0; i < M; i++)
	{
		scanf("%s",st[i].name);
		scanf("%d",&st[i].sex);
		scanf("%d",&st[i].age);
	}
}

5.构造一个单链表(每个结点包含2个字段:整数信息、后继指针)。编写函数,删除该单链表中相邻结点包含相同整数信息的重复结点,仅保留一个。

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

typedef struct node {
    int data;
    struct node* next;
} node;

void insert(node** head, int data) {
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;

    if (*head == NULL) {
        *head = new_node;
    } else {
        struct node* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = new_node;
    }
}

void removeDuplicates(node* head) {
    if (head == NULL) {
        return;
    }
    
    node* current = head;
    while (current->next != NULL) {
        if (current->data == current->next->data) {
            node* temp = current->next;
            current->next = temp->next;
            free(temp);
        } else {
            current = current->next;
        }
    }
}

void displayList(node* head) {
    node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

void freeList(node* head) {
    node* current = head;
    while (current != NULL) {
        node* temp = current;
        current = current->next;
        free(temp);
    }
}

int main() {
    node* head = NULL;
    insert(&head, 1);
    insert(&head, 2);
    insert(&head, 2);
    insert(&head, 3);
    insert(&head, 3);
    insert(&head, 4);
    insert(&head, 5);
    insert(&head, 5);
    insert(&head, 5);
    displayList(head);
    removeDuplicates(head);
    displayList(head);
    freeList(head);
    return 0;
}
相关推荐
mochensage8 分钟前
C++信息学竞赛中常用函数的一般用法
java·c++·算法
chengooooooo14 分钟前
leetcode Top100 238. 除自身以外数组的乘积|数组系列
算法·leetcode
GUIQU.30 分钟前
【每日一题 | 2025年6.2 ~ 6.8】第16届蓝桥杯部分偏简单题
算法·蓝桥杯·每日一题
weixin_527550401 小时前
初级程序员入门指南
javascript·python·算法
乄夜2 小时前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
丶Darling.3 小时前
26考研 | 王道 | 计算机组成原理 | 四、指令系统
考研·性能优化
思捻如枫3 小时前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
嘉陵妹妹3 小时前
深度优先算法学习
学习·算法·深度优先
GalaxyPokemon4 小时前
LeetCode - 53. 最大子数组和
算法·leetcode·职场和发展
小猫咪怎么会有坏心思呢4 小时前
华为OD机考 - 水仙花数 Ⅰ(2025B卷 100分)
数据结构·链表·华为od