快速构建一个包含A-Z的所有字母的数组

[...Array(26)].map((v,i) => String.fromCharCode(i + 65))
 
//["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

以下是使用霍夫曼编码进行压缩的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义霍夫曼树的结构体 struct huffman_tree { int weight; int parent; int lchild; int rchild; }; // 定义霍夫曼编码的结构体 struct huffman_code { char symbol; char code[100]; }; // 根据概率数组构建霍夫曼树 void build_huffman_tree(int *w, int n, struct huffman_tree *ht) { int i, j, min1, min2; for (i = 0; i < 2 * n - 1; i++) { ht[i].weight = i < n ? w[i] : 0; ht[i].parent = -1; ht[i].lchild = -1; ht[i].rchild = -1; } for (i = n; i < 2 * n - 1; i++) { min1 = min2 = 0; for (j = 0; j < i; j++) { if (ht[j].parent == -1) { if (ht[j].weight < ht[min1].weight) { min2 = min1; min1 = j; } else if (ht[j].weight < ht[min2].weight) { min2 = j; } } } ht[min1].parent = i; ht[min2].parent = i; ht[i].lchild = min1; ht[i].rchild = min2; ht[i].weight = ht[min1].weight + ht[min2].weight; } } // 根据霍夫曼树计算每个字符的霍夫曼编码 void get_huffman_code(struct huffman_tree *ht, int n, struct huffman_code *hc) { int i, j, p; char code[100]; code[n - 1] = '\0'; for (i = 0; i < n; i++) { hc[i].symbol = i + 'a'; p = i; j = n - 1; while (ht[p].parent != -1) { if (ht[ht[p].parent].lchild == p) { code[--j] = '0'; } else { code[--j] = '1'; } p = ht[p].parent; } strcpy(hc[i].code, &code[j]); } } int main() { int weights[26]; int i, j, length, total_bits; char sequence[1000]; struct huffman_tree ht[51]; struct huffman_code hc[26]; // 读取输入数据 for (i = 0; i < 26; i++) { scanf("%d", &weights[i]); } scanf("%s", sequence); length = strlen(sequence); // 构建霍夫曼树并计算每个字符的霍夫曼编码 build_huffman_tree(weights, 26, ht); get_huffman_code(ht, 26, hc); // 计算压缩后的比特数 total_bits = 0; for (i = 0; i < length; i++) { for (j = 0; j < 26; j++) { if (sequence[i] == hc[j].symbol) { total_bits += strlen(hc[j].code); break; } } } printf("%d\n", total_bits); return 0; } ``` 该程序的输入格式与要求相同,输出为使用霍夫曼编码压缩后的总比特数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值