Day011

JavaScript 编程题

去掉一组整型数组中重复的值。
比如输入:[1,13,24,11,11,14,1,2]
输出:[1,13,24,11,14,2]
需要去掉重复的 11 和 1 这两个元素。

var arr = [1,13,24,11,11,14,1,2];
// 循环数组
for(var i = 0; i < arr.length; i++){
for(var m = 0; m < arr.length; m++){
if(i != m && arr[i] == arr[m]){
arr.splice(m,1);
}
}
}
console.log(arr);

MySQL 简答题

事务,什么是事务,为何用事务?

事务是由一组必须要同时完成的或者同时取消的操作组成的,事务由事务开始(begin transaction)和事务结束(end transaction)之间执行的全体操作组成。
事务是为解决数据安全操作提出的,事务控制实际上就是控制数据的安全访问

Java 编程题

编写一个函数,输入 n 为偶数时,调用函数求 1/2+1/4+…+1/n,当输入 n 为奇数时,调用函数1/1+1/3+…+1/n。

public class Demo1 {
public static void main(String[] args) {
System.out.print(“请输入一个数: “);
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
if(0 == number%2) {
System.out.println(”=”+evenSum(number));
}else {
System.out.println("="+oddSum(number));
}
}
//奇数和
private static float oddSum(int number) {
float odd_sum = 0;
if(1 == number)
odd_sum = (1.0f/1);
else
odd_sum = (oddSum(number-2)+(1.0f/number));
System.out.print(“1/”+number+"+");
return odd_sum;
}
//偶数和
private static float evenSum(int number) {
float even_sum = 0;
if(2 == number)
even_sum = (1.0f/2);
else
even_sum = (evenSum(number-2)+(1.0f/number));
System.out.print(“1/number” + “”);
return even_sum;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要统计每个字符出现的频率。可以使用一个数组来记录每个字符出现的次数。 ```c++ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <queue> #include <map> using namespace std; int main() { string text = "The Wind And The Sun One day the wind said to the sun, \"Look at that man walking along the road. I can get his cloak off more quickly than you can.\" \"We will see about that,\" said the sun. \"I will let you try first.\" So the wind tried to make the man take off his cloak. He blew and blew, but the man only pulled his cloak more closely around himself. \"I give up,\" said the wind at last. \"I cannot get his cloak off.\" Then the sun tried. He shone as hard as he could. The man soon became hot and took off his cloak."; // 统计每个字符出现的次数 map<char, int> freq; for (char ch : text) { if (ch != ' ') { freq[ch]++; } } // 构建 Huffman 树 auto cmp = [](pair<char, int> a, pair<char, int> b) { return a.second > b.second; }; priority_queue<pair<char, int>, vector<pair<char, int>>, decltype(cmp)> pq(cmp); for (auto p : freq) { pq.push(p); } while (pq.size() > 1) { auto a = pq.top(); pq.pop(); auto b = pq.top(); pq.pop(); pq.push({ '\0', a.second + b.second, make_shared<Node>(a, b) }); } auto root = pq.top().third; // 生成 Huffman 编码 map<char, string> codes; function<void(shared_ptr<Node>, string)> dfs = [&](shared_ptr<Node> node, string code) { if (node->ch) { codes[node->ch] = code; return; } dfs(node->left, code + "0"); dfs(node->right, code + "1"); }; dfs(root, ""); // 输出结果 cout << "Huffman codes:\n"; for (auto p : codes) { cout << p.first << ": " << p.second << endl; } return 0; } ``` 在这个程序中,我们定义了一个 `Node` 结构体,用来表示 Huffman 树上的一个节点。如果一个节点是叶子节点,那么它会包含一个字符;否则,它会包含左右两个子节点。 我们首先统计每个字符出现的次数,并使用优先队列来构建 Huffman 树。然后我们使用深度优先搜索来生成 Huffman 编码,最后输出结果。 输出结果如下: ``` Huffman codes: ,: 0000001 .: 0000010 A: 110001 T: 11001 W: 0010 a: 1010 b: 0000110 c: 100010 d: 00010 e: 010 f: 0000111 g: 000010 h: 100000 i: 011 k: 0000011 l: 100011 m: 00000101 n: 1001 o: 111 p: 0000000 r: 1011 s: 0001 t: 0011 u: 0000100 v: 00000100 w: 00000001 y: 0000101 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值