add
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#define MAX_CAPACITY 150
|
||||
#define MAX_ITEMS 7
|
||||
typedef struct {// 物品结构体,包含重量、价值和性价比
|
||||
int weight;
|
||||
int value;
|
||||
double ratio; // 性价比
|
||||
} Item;
|
||||
void sortItems(Item items[], int n) {// 计算性价比并进行排序
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (items[i].ratio < items[j].ratio) {
|
||||
// 交换物品
|
||||
Item temp = items[i];
|
||||
items[i] = items[j];
|
||||
items[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 初始化物品
|
||||
Item items[MAX_ITEMS] = { {35, 10, 0}, {30, 40, 0}, {60, 30, 0},
|
||||
{50, 50, 0}, {40, 35, 0}, {10, 40, 0}, {25, 30, 0} };
|
||||
for (int i = 0; i < MAX_ITEMS; i++) { // 计算性价比
|
||||
items[i].ratio = (double)items[i].value / items[i].weight;
|
||||
}
|
||||
sortItems(items, MAX_ITEMS);// 按性价比排序
|
||||
int capacity = MAX_CAPACITY;
|
||||
int maxValue = 0;
|
||||
printf("背包装入物品:\n"); // 装入背包
|
||||
for (int i = 0; i < MAX_ITEMS; i++) {
|
||||
if (items[i].weight <= capacity) {
|
||||
printf("重量为:%d的物品被选中了\n", items[i].weight);
|
||||
maxValue += items[i].value;
|
||||
capacity -= items[i].weight;
|
||||
}
|
||||
}
|
||||
printf("最大价值为:%d\n", maxValue);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#define MAX_CAPACITY 15
|
||||
#define MAX_ITEMS 5
|
||||
typedef struct {// 物品结构体,包含重量、价值和性价比
|
||||
int weight;
|
||||
int value;
|
||||
double ratio; // 性价比
|
||||
} Item;
|
||||
void sortItems(Item items[], int n) {// 计算性价比并进行排序
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (items[i].ratio < items[j].ratio) {
|
||||
// 交换物品
|
||||
Item temp = items[i];
|
||||
items[i] = items[j];
|
||||
items[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 初始化物品
|
||||
Item items[MAX_ITEMS] = { {10, 8, 0}, {2,1, 0}, {5, 6, 0},
|
||||
{5,3, 0}, {7, 7, 0} };
|
||||
for (int i = 0; i < MAX_ITEMS; i++) { // 计算性价比
|
||||
items[i].ratio = (double)items[i].value / items[i].weight;
|
||||
}
|
||||
sortItems(items, MAX_ITEMS);// 按性价比排序
|
||||
int capacity = MAX_CAPACITY;
|
||||
int maxValue = 0;
|
||||
printf("背包装入物品:\n"); // 装入背包
|
||||
for (int i = 0; i < MAX_ITEMS; i++) {
|
||||
if (items[i].weight <= capacity) {
|
||||
printf("重量为:%d的物品被选中了\n", items[i].weight);
|
||||
maxValue += items[i].value;
|
||||
capacity -= items[i].weight;
|
||||
}
|
||||
}
|
||||
printf("最大价值为:%d\n", maxValue);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,47 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
typedef struct { float weight; int index;} Element;
|
||||
int compare(const void *a, const void *b) {
|
||||
const Element *elemA = (const Element *)a;
|
||||
const Element *elemB = (const Element *)b;
|
||||
return (elemA->weight > elemB->weight) - (elemA->weight < elemB->weight);
|
||||
}
|
||||
// 最优装载函数
|
||||
float loading(float capacity, float weights[], int count, int x[]) {
|
||||
Element *elements = malloc(count * sizeof(Element));
|
||||
for (int i = 0; i < count; i++) { // 初始化元素
|
||||
elements[i].weight = weights[i];
|
||||
elements[i].index = i;
|
||||
}
|
||||
qsort(elements, count, sizeof(Element), compare); // 排序元素
|
||||
// 初始化装载情况
|
||||
for (int i = 0; i < count; i++) { x[i] = 0; }
|
||||
float totalWeight = 0;
|
||||
// 贪心选择
|
||||
for (int i = 0; i < count && elements[i].weight <= capacity; i++) {
|
||||
totalWeight += elements[i].weight;
|
||||
capacity -= elements[i].weight;
|
||||
x[elements[i].index] = 1; // 标记被装载的集装箱
|
||||
}
|
||||
free(elements); // 释放动态分配的内存
|
||||
return totalWeight;
|
||||
}
|
||||
int main() {
|
||||
float weights[] = {20, 30, 26, 15, 30, 40, 35,50,25};
|
||||
float capacity = 100;
|
||||
int count = sizeof(weights) / sizeof(weights[0]);
|
||||
int *x = malloc(count * sizeof(int));
|
||||
|
||||
float optimalWeight = loading(capacity, weights, count, x);
|
||||
|
||||
printf("最优得到装载重量为:%.2f\n", optimalWeight);
|
||||
printf("被装载的集装箱序号为(下标从0开始):");
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (x[i] == 1) {
|
||||
printf("%d ", i);
|
||||
}
|
||||
}
|
||||
free(x); // 释放动态分配的内存
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
typedef struct {
|
||||
int age;
|
||||
} CompareObj;
|
||||
|
||||
// 比较函数,用于 qsort
|
||||
int compare(const void *a, const void *b) {
|
||||
CompareObj *objA = (CompareObj *)a;
|
||||
CompareObj *objB = (CompareObj *)b;
|
||||
return (objA->age - objB->age); // 按从小到大排序
|
||||
}
|
||||
void printObj(CompareObj obj) {
|
||||
printf("我的年龄:%d\n", obj.age);
|
||||
}
|
||||
int main() {
|
||||
CompareObj objs[3]; // 初始化对象数组
|
||||
objs[0].age = 20; objs[1].age = 30; objs[2].age = 25;
|
||||
qsort(objs, 3, sizeof(CompareObj), compare);
|
||||
for (int i = 0; i < 3; i++)
|
||||
printObj(objs[i]);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,224 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_TREE_HT 256
|
||||
|
||||
// 定义 Huffman 树的节点结构
|
||||
typedef struct Node {
|
||||
int weight; // 节点的权重,即字符的频率
|
||||
char data; // 存储字符
|
||||
struct Node *left, *right; // 左右子节点
|
||||
} Node;
|
||||
|
||||
// 比较函数,用于 qsort 按照权重排序
|
||||
int compare(const void* a, const void* b) {
|
||||
return (*(Node**)a)->weight - (*(Node**)b)->weight;
|
||||
}
|
||||
|
||||
// 创建一个新的节点
|
||||
Node* newNode(char data, int weight) {
|
||||
Node* temp = (Node*)malloc(sizeof(Node));
|
||||
temp->left = temp->right = NULL;
|
||||
temp->data = data;
|
||||
temp->weight = weight;
|
||||
return temp;
|
||||
}
|
||||
|
||||
// 构建 Huffman 树
|
||||
Node* buildHuffmanTree(char data[], int weight[], int size) {
|
||||
// 创建一个指向节点的数组
|
||||
Node** nodeArr = (Node**)malloc(size * sizeof(Node*));
|
||||
// 将每个字符及其权重转化为节点并存入数组
|
||||
for (int i = 0; i < size; ++i) {
|
||||
nodeArr[i] = newNode(data[i], weight[i]);
|
||||
}
|
||||
// 使用 qsort 按照权重排序节点
|
||||
qsort(nodeArr, size, sizeof(Node*), compare);
|
||||
// 构建 Huffman 树
|
||||
while (size > 1) {
|
||||
// 取出两个最小的节点
|
||||
Node* left = nodeArr[0];
|
||||
Node* right = nodeArr[1];
|
||||
// 创建一个新的父节点,权重为两个子节点的权重之和
|
||||
Node* parent = newNode('\0', left->weight + right->weight);
|
||||
parent->left = left;
|
||||
parent->right = right;
|
||||
// 将父节点插入到 heap 中
|
||||
nodeArr[0] = parent;
|
||||
nodeArr[1] = nodeArr[size - 1]; // 将最后一个节点放到第二个位置
|
||||
size--;
|
||||
// 重新排序数组
|
||||
qsort(nodeArr, size, sizeof(Node*), compare);
|
||||
}
|
||||
Node* root = nodeArr[0];
|
||||
free(nodeArr);
|
||||
return root;
|
||||
}
|
||||
|
||||
// 打印 Huffman 编码
|
||||
void printHuffmanCodes(Node* root, int arr[], int top, char* codes[]) {
|
||||
if (root->left) {
|
||||
arr[top] = 0;
|
||||
printHuffmanCodes(root->left, arr, top + 1, codes);
|
||||
}
|
||||
|
||||
if (root->right) {
|
||||
arr[top] = 1;
|
||||
printHuffmanCodes(root->right, arr, top + 1, codes);
|
||||
}
|
||||
|
||||
// 如果是叶节点,打印字符和对应的编码
|
||||
if (!root->left && !root->right) {
|
||||
codes[root->data] = (char*)malloc((top + 1) * sizeof(char));
|
||||
if (!codes[root->data]) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(1);
|
||||
}
|
||||
for (int i = 0; i < top; ++i)
|
||||
codes[root->data][i] = '0' + arr[i];
|
||||
codes[root->data][top] = '\0';
|
||||
printf("%c: %s\n", root->data, codes[root->data]);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取字符的 Huffman 编码
|
||||
const char* getHuffmanCode(Node* root, char ch, int arr[], int top, char* codes[]) {
|
||||
if (codes[ch])
|
||||
return codes[ch];
|
||||
|
||||
if (root->left) {
|
||||
arr[top] = 0;
|
||||
const char* code = getHuffmanCode(root->left, ch, arr, top + 1, codes);
|
||||
if (code)
|
||||
return code;
|
||||
}
|
||||
|
||||
if (root->right) {
|
||||
arr[top] = 1;
|
||||
const char* code = getHuffmanCode(root->right, ch, arr, top + 1, codes);
|
||||
if (code)
|
||||
return code;
|
||||
}
|
||||
|
||||
// 如果是叶节点,检查是否是我们要找的字符
|
||||
if (!root->left && !root->right && root->data == ch) {
|
||||
codes[root->data] = (char*)malloc((top + 1) * sizeof(char));
|
||||
if (!codes[root->data]) {
|
||||
fprintf(stderr, "Memory allocation failed\n");
|
||||
exit(1);
|
||||
}
|
||||
for (int i = 0; i < top; ++i)
|
||||
codes[root->data][i] = '0' + arr[i];
|
||||
codes[root->data][top] = '\0';
|
||||
return codes[root->data];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 编码函数:使用 Huffman 树编码文本
|
||||
void encode(Node* root, const char* str, char* encodedStr, char* codes[]) {
|
||||
int arr[MAX_TREE_HT], top = 0;
|
||||
printf("Huffman Codes:\n");
|
||||
printHuffmanCodes(root, arr, top, codes);
|
||||
|
||||
printf("\nEncoded Text: ");
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
const char* code = getHuffmanCode(root, str[i], arr, 0, codes);
|
||||
if (code) {
|
||||
printf("%s", code);
|
||||
strcat(encodedStr, code); // 将每个字符的编码拼接到最终的编码字符串中
|
||||
} else {
|
||||
fprintf(stderr, "Character '%c' not found in Huffman tree\n", str[i]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// 解码函数:从 Huffman 树解码编码文本
|
||||
void decode(Node* root, const char* encodedStr) {
|
||||
Node* current = root; // 从根节点开始
|
||||
printf("\nDecoded Text: ");
|
||||
|
||||
for (int i = 0; encodedStr[i] != '\0'; i++) {
|
||||
// 根据编码字符串的每个字符决定树的遍历方向
|
||||
if (encodedStr[i] == '0') {
|
||||
current = current->left; // 向左子节点移动
|
||||
} else if (encodedStr[i] == '1') {
|
||||
current = current->right; // 向右子节点移动
|
||||
}
|
||||
|
||||
// 如果到达叶节点,输出字符并返回根节点
|
||||
if (!current->left && !current->right) {
|
||||
printf("%c", current->data);
|
||||
current = root; // 重置为根节点,准备解码下一个字符
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// 释放 Huffman 树的内存
|
||||
void freeHuffmanTree(Node* node) {
|
||||
if (node == NULL)
|
||||
return;
|
||||
|
||||
freeHuffmanTree(node->left);
|
||||
freeHuffmanTree(node->right);
|
||||
free(node);
|
||||
}
|
||||
|
||||
// 释放 Huffman 编码的内存
|
||||
void freeHuffmanCodes(char* codes[]) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
if (codes[i]) {
|
||||
free(codes[i]);
|
||||
codes[i] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const char* text = "hello huffman coding";
|
||||
|
||||
// 计算每个字符的频率
|
||||
int freq[256] = {0};
|
||||
for (int i = 0; text[i] != '\0'; i++) {
|
||||
freq[(unsigned char)text[i]]++;
|
||||
}
|
||||
|
||||
// 构建字符和频率的数组
|
||||
char data[256];
|
||||
int frequencies[256];
|
||||
int size = 0;
|
||||
for (int i = 0; i < 256; i++) {
|
||||
if (freq[i] > 0) {
|
||||
data[size] = (char)i;
|
||||
frequencies[size] = freq[i];
|
||||
size++;
|
||||
}
|
||||
}
|
||||
|
||||
// 构建 Huffman 树
|
||||
Node* root = buildHuffmanTree(data, frequencies, size);
|
||||
|
||||
// 初始化 Huffman 编码数组
|
||||
char* codes[256] = {NULL};
|
||||
|
||||
// 编码
|
||||
char encodedText[MAX_TREE_HT * strlen(text)];
|
||||
memset(encodedText, 0, sizeof(encodedText));
|
||||
encode(root, text, encodedText, codes);
|
||||
|
||||
// 解码
|
||||
decode(root, encodedText);
|
||||
|
||||
// 释放 Huffman 树的内存
|
||||
freeHuffmanTree(root);
|
||||
|
||||
// 释放 Huffman 编码的内存
|
||||
freeHuffmanCodes(codes);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,100 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct Node {
|
||||
int weight;
|
||||
struct Node *left, *right;
|
||||
} Node;
|
||||
|
||||
// 比较函数用于 qsort
|
||||
int compare(const void* a, const void* b) {
|
||||
return (*(Node**)a)->weight - (*(Node**)b)->weight;
|
||||
}
|
||||
|
||||
// 创建新节点
|
||||
Node* createNode(int weight) {
|
||||
Node* newNode = (Node*)malloc(sizeof(Node));
|
||||
newNode->weight = weight;
|
||||
newNode->left = newNode->right = NULL;
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// 构建哈夫曼树
|
||||
Node* createHuffmanTree(int* weights, int size) {
|
||||
Node** nodes = malloc(size * sizeof(Node*));
|
||||
for (int i = 0; i < size; i++) {
|
||||
nodes[i] = createNode(weights[i]);
|
||||
}
|
||||
while (size > 1) {
|
||||
qsort(nodes, size, sizeof(Node*), compare);// 排序节点数组
|
||||
// 创建父节点
|
||||
Node* parent = createNode(nodes[0]->weight + nodes[1]->weight);
|
||||
parent->left = nodes[0];
|
||||
parent->right = nodes[1];
|
||||
|
||||
// 替换前两个最小节点
|
||||
nodes[0] = parent;
|
||||
for (int i = 1; i < size - 1; i++) {
|
||||
nodes[i] = nodes[i + 1];
|
||||
}
|
||||
size--;
|
||||
}
|
||||
Node* root = nodes[0];
|
||||
free(nodes);
|
||||
return root;
|
||||
}
|
||||
|
||||
// 递归生成哈夫曼编码
|
||||
void encode(Node* node, char* code, int depth) {
|
||||
if (node->left == NULL && node->right == NULL) {
|
||||
code[depth] = '\0';//终止字符串
|
||||
printf("权重: %d, 编码: %s\n", node->weight, code);
|
||||
return;
|
||||
}
|
||||
code[depth] = '0';
|
||||
encode(node->left, code, depth + 1);
|
||||
code[depth] = '1';
|
||||
encode(node->right, code, depth + 1);
|
||||
}
|
||||
|
||||
// 解码函数
|
||||
void decode(Node* root, const char* encoded) {
|
||||
Node* current = root;
|
||||
while (*encoded) {
|
||||
if (*encoded == '0') {
|
||||
current = current->left;
|
||||
} else {
|
||||
current = current->right;
|
||||
}
|
||||
// 到达叶子节点,打印权重并返回根节点
|
||||
if (current->left == NULL && current->right == NULL) {
|
||||
printf("解码权重: %d\n", current->weight);
|
||||
current = root; // 回到根节点
|
||||
}
|
||||
encoded++;// 移动到下一个字符
|
||||
}
|
||||
}
|
||||
// 释放内存
|
||||
void freeTree(Node* node) {
|
||||
if (node) {
|
||||
freeTree(node->left);
|
||||
freeTree(node->right);
|
||||
free(node);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int weights[] = {2, 3, 7, 9, 18, 25};
|
||||
int size = sizeof(weights) / sizeof(weights[0]);
|
||||
Node* root = createHuffmanTree(weights, size);
|
||||
char code[100]; // 假设编码长度不会超过100
|
||||
printf("编码:\n");
|
||||
encode(root, code, 0);
|
||||
|
||||
const char* encoded = "0010101111";
|
||||
printf("解码-0010101111:\n");
|
||||
decode(root, encoded);
|
||||
|
||||
freeTree(root);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,91 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
typedef struct Node
|
||||
{
|
||||
int weight;
|
||||
struct Node *lChild;
|
||||
struct Node *rChild;
|
||||
} Node;
|
||||
|
||||
// 比较函数,用于优先队列
|
||||
int compare(const void *a, const void *b)
|
||||
{
|
||||
return (*(Node **)a)->weight - (*(Node **)b)->weight;
|
||||
}
|
||||
|
||||
// 创建新的节点
|
||||
Node *createNode(int weight)
|
||||
{
|
||||
Node *newNode = (Node *)malloc(sizeof(Node));
|
||||
newNode->weight = weight;
|
||||
newNode->lChild = NULL;
|
||||
newNode->rChild = NULL;
|
||||
return newNode;
|
||||
}
|
||||
// 构建哈夫曼树
|
||||
Node *createHuffman(int *weights, int size)
|
||||
{
|
||||
Node **nodeArray = (Node **)malloc(size * sizeof(Node *));
|
||||
// 初始化节点数组
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
nodeArray[i] = createNode(weights[i]);
|
||||
printf("%d,", nodeArray[i]->weight);
|
||||
}
|
||||
qsort(nodeArray, size, sizeof(Node *), compare);
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
printf("%d-", nodeArray[i]->weight);
|
||||
}
|
||||
// 使用qsort实现优先队列
|
||||
for (int i = size; i > 1; i--)
|
||||
{
|
||||
qsort(nodeArray, i, sizeof(Node *), compare);
|
||||
// 找到最右边两个节点
|
||||
Node *left = nodeArray[i - 1];
|
||||
Node *right = nodeArray[i - 2];
|
||||
// 创建新父节点
|
||||
Node *parent = createNode(left->weight + right->weight);
|
||||
parent->lChild = left;
|
||||
parent->rChild = right;
|
||||
// 将新节点放入数组中
|
||||
nodeArray[i - 2] = parent;
|
||||
}
|
||||
|
||||
Node *root = nodeArray[0];
|
||||
free(nodeArray);
|
||||
return root;
|
||||
}
|
||||
// 前序遍历输出
|
||||
void output(Node *head)
|
||||
{
|
||||
if (head == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
printf("%d\n", head->weight);
|
||||
output(head->lChild);
|
||||
output(head->rChild);
|
||||
}
|
||||
// 释放树的内存
|
||||
void freeTree(Node *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
freeTree(root->lChild);
|
||||
freeTree(root->rChild);
|
||||
free(root);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int weights[] = {2, 18, 9, 3, 7,25};
|
||||
int size = sizeof(weights) / sizeof(weights[0]);
|
||||
Node *huffmanTree = createHuffman(weights, size);
|
||||
printf("前序遍历halftree:\n");
|
||||
output(huffmanTree);
|
||||
// 释放树的内存
|
||||
freeTree(huffmanTree);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
f: 00
|
||||
n: 010
|
||||
m: 0110
|
||||
e: 0111
|
||||
h: 100
|
||||
: 1010
|
||||
a: 1011
|
||||
u: 1100
|
||||
o: 1101
|
||||
l: 111
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
TreeNode* createNode(int value) {// 创建新节点
|
||||
TreeNode *newNode = (TreeNode*)malloc(sizeof(TreeNode));
|
||||
newNode->val = value;
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
return newNode;
|
||||
}
|
||||
void preorderTraversal(TreeNode *root) {
|
||||
if (root == NULL) return;
|
||||
printf("%d ", root->val); // 访问根节点
|
||||
preorderTraversal(root->left); // 遍历左子树
|
||||
preorderTraversal(root->right); // 遍历右子树
|
||||
}
|
||||
void inorderTraversal(TreeNode *root) {
|
||||
if (root == NULL) return;
|
||||
inorderTraversal(root->left); // 遍历左子树
|
||||
printf("%d ", root->val); // 访问根节点
|
||||
inorderTraversal(root->right); // 遍历右子树
|
||||
}
|
||||
void postorderTraversal(TreeNode *root) {
|
||||
if (root == NULL) return;
|
||||
postorderTraversal(root->left); // 遍历左子树
|
||||
postorderTraversal(root->right); // 遍历右子树
|
||||
printf("%d ", root->val); // 访问根节点
|
||||
}
|
||||
int main() {
|
||||
// 创建二叉树
|
||||
TreeNode *root = createNode(1);
|
||||
root->left = createNode(2);
|
||||
root->right = createNode(3);
|
||||
root->left->left = createNode(4);
|
||||
root->left->right = createNode(5);
|
||||
|
||||
printf("前序遍历: ");
|
||||
preorderTraversal(root);
|
||||
printf("\n");
|
||||
printf("中序遍历: ");
|
||||
inorderTraversal(root);
|
||||
printf("\n");
|
||||
printf("后序遍历: ");
|
||||
postorderTraversal(root);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
int greedySelector(int s[], int f[], bool a[], int n) {
|
||||
a[0] = true; // 安排第一个活动,标记为true
|
||||
int j = 0; // 上一个被安排的活动的索引
|
||||
int count = 1; // 已安排活动的个数
|
||||
for (int i = 1; i < n; i++) {
|
||||
// 检验当前最早结束的活动的开始时间是否晚于前一个活动的结束时间
|
||||
if (s[i] >= f[j]) {
|
||||
a[i] = true; // 标记活动为已安排
|
||||
j = i; // 更新上一个活动的索引
|
||||
count++; // 记已安排活动的个数
|
||||
} else {
|
||||
a[i] = false; // 标记活动为未安排
|
||||
}
|
||||
}
|
||||
return count; // 返回已安排活动的个数
|
||||
}
|
||||
int main() {
|
||||
// 初始化数据s数组记录活动开始时间;f数组记录活动结束时间
|
||||
int s[] = {1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12};
|
||||
int f[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
|
||||
int n = sizeof(s) / sizeof(s[0]); // 计算活动的个数
|
||||
bool a[n]; // boolean型数组,表示活动安排情况
|
||||
int result = greedySelector(s, f, a, n);
|
||||
// 输出结果
|
||||
printf("选中的活动个数: %d\n", result);
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (a[i]) {
|
||||
printf("编号为:%d的活动被选中,", i + 1);
|
||||
printf("其开始时间为:%d,结束时间为:%d\n", s[i], f[i]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
int money[] = {100, 50, 20, 10, 5, 1};
|
||||
int n = 85;
|
||||
printf("要找的钱数为:%d\n", n);
|
||||
int num[6] = {0}; // 存储每种面值所需的数量
|
||||
for (int i = 0; i < 6; i++) {
|
||||
num[i] = n / money[i]; // 计算需要的面值数量
|
||||
n = n % money[i]; // 更新剩余的钱数
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
printf("需要%d元面值%d枚\n", money[i], num[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void makeGreedyChange(int m, int plan[][2], int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
plan[i][1] += m / plan[i][0]; // 计算需要的面值数量
|
||||
m = m % plan[i][0]; // 更新剩余的钱数
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
int money = 90;
|
||||
// 用一个二维数组 plan[6][2] 来存储每一种面值和其所对应的具体数目
|
||||
int plan[6][2] = { {50, 0}, {20, 0}, {10, 0}, {5, 0}, {2, 0}, {1, 0} };
|
||||
|
||||
makeGreedyChange(money, plan, 6);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
printf("%d元: %d张\n", plan[i][0], plan[i][1]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <float.h>
|
||||
#include <stdbool.h>
|
||||
#define MAX_NODES 5
|
||||
void dijkstra(int v, float a[MAX_NODES][MAX_NODES],
|
||||
float dist[MAX_NODES], int prev[MAX_NODES]) {
|
||||
int n = MAX_NODES; // 节点总数
|
||||
if (v < 0 || v >= n) { return; // 如果源节点不在有效范围内,返回
|
||||
}
|
||||
bool s[MAX_NODES] = {false}; // 标记节点是否已确定最短路径
|
||||
for (int i = 0; i < n; i++) { // 初始化
|
||||
dist[i] = a[v][i]; s[i] = false;
|
||||
if (dist[i] == FLT_MAX) { prev[i] = -1; // 如果没有路径,prev 设置为 -1
|
||||
} else { prev[i] = v; // 否则,前驱节点为源节点
|
||||
}
|
||||
}
|
||||
dist[v] = 0; // 源节点的距离是0
|
||||
s[v] = true; // 源节点加入已确定集合
|
||||
for (int i = 1; i < n; i++) { // Dijkstra 算法主体
|
||||
float temp = FLT_MAX; int u = v;
|
||||
// 选择未访问的最短路径节点
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (!s[j] && dist[j] < temp) {
|
||||
u = j;
|
||||
temp = dist[j];
|
||||
}
|
||||
}
|
||||
s[u] = true; // 确定最短路径节点 u
|
||||
// 更新与节点 u 相邻的节点的最短路径
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (!s[j] && a[u][j] < FLT_MAX) {
|
||||
float newdist = dist[u] + a[u][j];
|
||||
if (newdist < dist[j]) {
|
||||
dist[j] = newdist;
|
||||
prev[j] = u; // 更新前驱节点
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
// 定义图的邻接矩阵 (FLT_MAX 表示没有路径)
|
||||
float a[MAX_NODES][MAX_NODES] = {
|
||||
{0, 8, 1, 2, FLT_MAX},
|
||||
{8, 0, FLT_MAX, 3, FLT_MAX},
|
||||
{1, FLT_MAX, 0, 2, 3},
|
||||
{2, 3, 2, 0, 3},
|
||||
{FLT_MAX, FLT_MAX, 3, 3, 0}
|
||||
};
|
||||
float dist[MAX_NODES]; // 存储从源点到各个节点的最短距离
|
||||
int prev[MAX_NODES]; // 存储每个节点的前驱节点
|
||||
// 执行 Dijkstra 算法,从节点 0 开始
|
||||
dijkstra(0, a, dist, prev);
|
||||
// 输出源点到其他节点的最短距离
|
||||
for (int i = 1; i < MAX_NODES; i++) {
|
||||
if (dist[i] == FLT_MAX) {
|
||||
printf("源点到%d没有路径\n", i);
|
||||
} else {
|
||||
printf("源点到%d的最短距离为:%.2f\n", i, dist[i]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,61 @@
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void dijkstra(int n, int graph[n][n], int source, int dist[n], int parent[n], bool visited[n]) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
dist[i] = INT_MAX; // 初始距离为无穷大
|
||||
parent[i] = -1; // 前驱节点初始化为 -1
|
||||
visited[i] = false; // 所有节点初始未访问
|
||||
}
|
||||
dist[source] = 0; // 源节点的距离为 0
|
||||
for (int i = 0; i < n - 1; i++) {// 寻找未访问的最小距离节点
|
||||
int u = -1;
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (!visited[j] && (u == -1 || dist[j] < dist[u])) {
|
||||
u = j;
|
||||
}
|
||||
}
|
||||
visited[u] = true;// 标记节点 u 为已访问
|
||||
// 更新与节点 u 相邻的未访问节点的最短路径
|
||||
for (int v = 0; v < n; v++) {
|
||||
if (graph[u][v] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
|
||||
dist[v] = dist[u] + graph[u][v];
|
||||
parent[v] = u; // 更新前驱节点
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void printPath(int parent[], int node) {
|
||||
if (node == -1) return; // 终止条件
|
||||
printPath(parent, parent[node]); // 递归打印前驱节点
|
||||
printf("%d ", node); // 打印当前节点
|
||||
}
|
||||
// 主函数
|
||||
int main() {
|
||||
int n = 5;
|
||||
int graph[5][5] = { // 邻接矩阵表示图
|
||||
{0, 10, INT_MAX, INT_MAX, 5},
|
||||
{10, 0, 1, INT_MAX, INT_MAX},
|
||||
{INT_MAX, 1, 0, 3, INT_MAX},
|
||||
{INT_MAX, INT_MAX, 3, 0, 8},
|
||||
{5, INT_MAX, INT_MAX, 8, 0}
|
||||
};
|
||||
int source = 0; // 设定源节点为 0
|
||||
int dist[n]; // 存储到每个节点的最短距离
|
||||
int parent[n]; // 存储每个节点的前驱节点
|
||||
bool visited[n]; // 标记节点是否已经访问
|
||||
// 运行 Dijkstra 算法
|
||||
dijkstra(n, graph, source, dist, parent, visited);
|
||||
// 输出最短路径结果
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (dist[i] == INT_MAX) {
|
||||
printf("到点 %d 不可直达.\n", i);
|
||||
} else {
|
||||
printf("源点到%d的最短路径是 %d,路径为: ", i, dist[i]);
|
||||
printPath(parent, i); // 打印路径
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user