change .cn to .com
This commit is contained in:
64
Html/apps/static/68bacdfadf5aeae0912f7f18-基础算法-二分.html
Normal file
64
Html/apps/static/68bacdfadf5aeae0912f7f18-基础算法-二分.html
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Document</title>
|
||||||
|
<!-- 你的其他样式 -->
|
||||||
|
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||||||
|
<script>
|
||||||
|
MathJax.config = {
|
||||||
|
tex: {
|
||||||
|
inlineMath: [['$', '$'], ['\(', '\)']]
|
||||||
|
},
|
||||||
|
svg: {
|
||||||
|
fontCache: 'global'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>二分查找与二分答案</h1>
|
||||||
|
<h2>二分查找</h2>
|
||||||
|
<h3>引入</h3>
|
||||||
|
<p>二分是一个很简单基础,但很重要的知识点,为以后许多高级的数据结构与算法铺垫。</p>
|
||||||
|
<p>下面是一个用二分的简单场景:</p>
|
||||||
|
<p>假设小明从0到1000之间选择了一个数字但不告诉你,你可以不断猜测这个数,每次猜测小明会告知你的猜测得过大还是过小,问最多几次就一定能猜中?</p>
|
||||||
|
<p>答案是利用二分查找的原理,猜测11次即可。</p>
|
||||||
|
<ol>
|
||||||
|
<li>对于0到1000的答案备选区,猜测中位数500,假设过小,</li>
|
||||||
|
<li>则对于501到1000的答案备选区,猜测750,假设过大</li>
|
||||||
|
<li>则对于501到749的答案备选区,猜测625,假设过小,</li>
|
||||||
|
<li>则对于626到749区间......</li>
|
||||||
|
<li>(688-749)</li>
|
||||||
|
<li>(718-749)</li>
|
||||||
|
<li>(734-749)</li>
|
||||||
|
<li>(742-749)</li>
|
||||||
|
<li>(746-749)</li>
|
||||||
|
<li>(748-749)</li>
|
||||||
|
<li>(749-749)</li>
|
||||||
|
</ol>
|
||||||
|
<p>在最差的情况下,第11次的答案备选区就一定长度为1了,也就是必然是答案。</p>
|
||||||
|
<p>因此如果序列是有序的,就可以通过二分查找快速定位所需要的数据。</p>
|
||||||
|
<h4>思考题(询问Agent以学习计算方法,或验证你的答案)</h4>
|
||||||
|
<p>对于上面那个题目,如果问题区间是1到4000,最差情况下需要猜测几次?</p>
|
||||||
|
<h3>练习:二分查找</h3>
|
||||||
|
<p>试试对于下面的题目,用代码实现一下二分查找。</p>
|
||||||
|
<h4>题目:有序数组寻址</h4>
|
||||||
|
<p>给出一个长度为n的有序数组(从小到大),有q次询问,对于每次询问,输出指定数在数组中的下标。如果不存在则输出-1。</p>
|
||||||
|
<h5>输入</h5>
|
||||||
|
<p>第一行一个整数n。(1<=n<=10^5)</p>
|
||||||
|
<p>第二行n个用空格分开的整数ai。(0<=ai<=10^8)</p>
|
||||||
|
<p>第三行一个整数q,表示询问的次数。(1<=q<=10^4)</p>
|
||||||
|
<p>后q行,每行一个整数b,表示询问的数。(0<=b<=10^8)</p>
|
||||||
|
<h5>输出</h5>
|
||||||
|
<p>q行,每行一个整数,对应每次询问的返回结果</p>
|
||||||
|
<h5>提示:</h5>
|
||||||
|
<p>完成代码后,通知Agent进行评测。</p>
|
||||||
|
<p>如果你还不完全会这个算法,询问Agent获取提示并进行学习。</p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
@@ -4,7 +4,7 @@ let system_message_idx = 0
|
|||||||
document.addEventListener('DOMContentLoaded', function () {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
data = window.appData;
|
data = window.appData;
|
||||||
console.log(data);
|
console.log(data);
|
||||||
socket = io('wss://hsamooc.cn/agent', {
|
socket = io('wss://hsamooc.com/agent', {
|
||||||
query: {
|
query: {
|
||||||
user_uuid: data.user_uuid,
|
user_uuid: data.user_uuid,
|
||||||
folder: data.folder
|
folder: data.folder
|
||||||
@@ -207,15 +207,133 @@ function sendMessage() {
|
|||||||
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
document.getElementById('chatbox').scrollTop = document.getElementById('chatbox').scrollHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
let voice_stream = null;
|
// let voice_stream = null;
|
||||||
let audio_chunks = [];
|
// let audio_chunks = [];
|
||||||
|
// let isRecording = false;
|
||||||
|
// let recordingInterval = null;
|
||||||
|
// //鼠标点击终止事件
|
||||||
|
// function handleMouseClick(event) {
|
||||||
|
// event.stopPropagation();
|
||||||
|
// stopRecording();
|
||||||
|
// }
|
||||||
|
// //发送语音
|
||||||
|
// const startAudioRecording = async () => {
|
||||||
|
// try {
|
||||||
|
// const stream = await navigator.mediaDevices.getUserMedia({
|
||||||
|
// audio: {
|
||||||
|
// sampleRate: 16000,
|
||||||
|
// channelCount: 1,
|
||||||
|
// echoCancellation: true,
|
||||||
|
// noiseSuppression: true
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// const mediaRecorder = new MediaRecorder(stream);
|
||||||
|
// const audioContext = new AudioContext({
|
||||||
|
// sampleRate: 16000
|
||||||
|
// });
|
||||||
|
|
||||||
|
// // Load the AudioWorklet
|
||||||
|
// await audioContext.audioWorklet.addModule('/static/js/audio-processor.js');
|
||||||
|
|
||||||
|
// const source = audioContext.createMediaStreamSource(stream);
|
||||||
|
// const workletNode = new AudioWorkletNode(audioContext, 'audio-processor');
|
||||||
|
// let audioChunks = [];
|
||||||
|
|
||||||
|
// workletNode.port.onmessage = (e) => {
|
||||||
|
// audioChunks.push(new Float32Array(e.data));
|
||||||
|
|
||||||
|
// if (audioChunks.length >= 16) {
|
||||||
|
// // 合并所有音频块
|
||||||
|
// const totalLength = audioChunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
||||||
|
// const mergedData = new Float32Array(totalLength);
|
||||||
|
// let offset = 0;
|
||||||
|
// for (const chunk of audioChunks) {
|
||||||
|
// mergedData.set(chunk, offset);
|
||||||
|
// offset += chunk.length;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const audioMessage = {
|
||||||
|
// id: window.data.user_uuid,
|
||||||
|
// type: 'audio',
|
||||||
|
// data: mergedData,
|
||||||
|
// sampleRate: 16000,
|
||||||
|
// timestamp: Date.now()
|
||||||
|
// };
|
||||||
|
// socket.emit('voice', audioMessage);
|
||||||
|
// console.log("voice sended")
|
||||||
|
// audioChunks = [];
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// source.connect(workletNode);
|
||||||
|
// workletNode.connect(audioContext.destination);
|
||||||
|
|
||||||
|
// mediaRecorder.value = {
|
||||||
|
// stop: () => {
|
||||||
|
// source.disconnect();
|
||||||
|
// workletNode.disconnect();
|
||||||
|
// stream.getTracks().forEach(track => track.stop());
|
||||||
|
// audioContext.close();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// isRecording.value = true;
|
||||||
|
// } catch (error) {
|
||||||
|
// console.error('Error starting audio recording:', error);
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
// function sendVoiceMessage() {
|
||||||
|
// startAudioRecording()
|
||||||
|
// //变换录音中图标
|
||||||
|
// document.getElementById('recordIcon').className = 'bi bi-record-circle';
|
||||||
|
// document.addEventListener('click', handleMouseClick);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// //停止录音
|
||||||
|
// function stopRecording() {
|
||||||
|
// if (!isRecording) return;
|
||||||
|
// isRecording = false;
|
||||||
|
// //取消监听
|
||||||
|
// document.removeEventListener('click', handleMouseClick);
|
||||||
|
// //取消定时器
|
||||||
|
// if (recordingInterval) {
|
||||||
|
// clearInterval(recordingInterval);
|
||||||
|
// recordingInterval = null;
|
||||||
|
// }
|
||||||
|
// //变换图标
|
||||||
|
// document.getElementById('recordIcon').className = 'bi bi-mic';
|
||||||
|
// //发送剩下的数据
|
||||||
|
// if (audio_chunks.length > 0) {
|
||||||
|
// const merged_data = new Uint8Array(audio_chunks.reduce((acc, chunk) => acc + chunk.length, 0));
|
||||||
|
// let offset = 0;
|
||||||
|
// for (const chunk of audio_chunks) {
|
||||||
|
// merged_data.set(new Uint8Array(chunk), offset);
|
||||||
|
// offset += chunk.length;
|
||||||
|
// }
|
||||||
|
// socket.emit('VoiceMessage', merged_data, { binary: true });
|
||||||
|
// audio_chunks = [];
|
||||||
|
// }
|
||||||
|
// overlay.style.display = 'none';
|
||||||
|
// //关闭音频流
|
||||||
|
// if (voice_stream) {
|
||||||
|
// voice_stream.stop_stream();
|
||||||
|
// voice_stream.close();
|
||||||
|
// voice_stream = null;
|
||||||
|
// }
|
||||||
|
// if (p) {
|
||||||
|
// p.terminate();
|
||||||
|
// p = null;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
//语音这一块//
|
||||||
|
let mediaRecorderInstance = null;
|
||||||
|
let audioContextInstance = null;
|
||||||
|
let workletNodeInstance = null;
|
||||||
|
let sourceInstance = null;
|
||||||
|
let streamInstance = null;
|
||||||
let isRecording = false;
|
let isRecording = false;
|
||||||
let recordingInterval = null;
|
|
||||||
//鼠标点击终止事件
|
|
||||||
function handleMouseClick(event) {
|
|
||||||
event.stopPropagation();
|
|
||||||
stopRecording();
|
|
||||||
}
|
|
||||||
//发送语音
|
//发送语音
|
||||||
const startAudioRecording = async () => {
|
const startAudioRecording = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -227,19 +345,31 @@ const startAudioRecording = async () => {
|
|||||||
noiseSuppression: true
|
noiseSuppression: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 保存实例到全局变量
|
||||||
|
streamInstance = stream;
|
||||||
const mediaRecorder = new MediaRecorder(stream);
|
const mediaRecorder = new MediaRecorder(stream);
|
||||||
|
mediaRecorderInstance = mediaRecorder;
|
||||||
|
|
||||||
const audioContext = new AudioContext({
|
const audioContext = new AudioContext({
|
||||||
sampleRate: 16000
|
sampleRate: 16000
|
||||||
});
|
});
|
||||||
|
audioContextInstance = audioContext;
|
||||||
|
|
||||||
// Load the AudioWorklet
|
// Load the AudioWorklet
|
||||||
await audioContext.audioWorklet.addModule('/static/js/audio-processor.js');
|
await audioContext.audioWorklet.addModule('/static/js/audio-processor.js');
|
||||||
|
|
||||||
const source = audioContext.createMediaStreamSource(stream);
|
const source = audioContext.createMediaStreamSource(stream);
|
||||||
|
sourceInstance = source;
|
||||||
|
|
||||||
const workletNode = new AudioWorkletNode(audioContext, 'audio-processor');
|
const workletNode = new AudioWorkletNode(audioContext, 'audio-processor');
|
||||||
let audioChunks = [];
|
workletNodeInstance = workletNode;
|
||||||
|
|
||||||
|
audioChunks = [];
|
||||||
|
|
||||||
workletNode.port.onmessage = (e) => {
|
workletNode.port.onmessage = (e) => {
|
||||||
|
if (!isRecording) return;
|
||||||
|
|
||||||
audioChunks.push(new Float32Array(e.data));
|
audioChunks.push(new Float32Array(e.data));
|
||||||
|
|
||||||
if (audioChunks.length >= 16) {
|
if (audioChunks.length >= 16) {
|
||||||
@@ -260,7 +390,7 @@ const startAudioRecording = async () => {
|
|||||||
timestamp: Date.now()
|
timestamp: Date.now()
|
||||||
};
|
};
|
||||||
socket.emit('voice', audioMessage);
|
socket.emit('voice', audioMessage);
|
||||||
console.log("voice sended")
|
console.log("voice sended");
|
||||||
audioChunks = [];
|
audioChunks = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -268,63 +398,75 @@ const startAudioRecording = async () => {
|
|||||||
source.connect(workletNode);
|
source.connect(workletNode);
|
||||||
workletNode.connect(audioContext.destination);
|
workletNode.connect(audioContext.destination);
|
||||||
|
|
||||||
mediaRecorder.value = {
|
isRecording = true;
|
||||||
stop: () => {
|
|
||||||
source.disconnect();
|
|
||||||
workletNode.disconnect();
|
|
||||||
stream.getTracks().forEach(track => track.stop());
|
|
||||||
audioContext.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isRecording.value = true;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error starting audio recording:', error);
|
console.error('Error starting audio recording:', error);
|
||||||
|
// 发生错误时清理资源
|
||||||
|
stopRecording();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
function sendVoiceMessage() {
|
|
||||||
startAudioRecording()
|
|
||||||
//变换录音中图标
|
|
||||||
document.getElementById('recordIcon').className = 'bi bi-record-circle';
|
|
||||||
document.addEventListener('click', handleMouseClick);
|
|
||||||
}
|
|
||||||
|
|
||||||
//停止录音
|
//停止录音
|
||||||
function stopRecording() {
|
function stopRecording() {
|
||||||
if (!isRecording) return;
|
if (!isRecording) return;
|
||||||
|
// 停止所有音频处理
|
||||||
|
if (workletNodeInstance) {
|
||||||
|
workletNodeInstance.disconnect();
|
||||||
|
workletNodeInstance = null;
|
||||||
|
}
|
||||||
|
if (sourceInstance) {
|
||||||
|
sourceInstance.disconnect();
|
||||||
|
sourceInstance = null;
|
||||||
|
}
|
||||||
|
if (audioContextInstance && audioContextInstance.state !== 'closed') {
|
||||||
|
audioContextInstance.close();
|
||||||
|
audioContextInstance = null;
|
||||||
|
}
|
||||||
|
// 停止媒体流
|
||||||
|
if (streamInstance) {
|
||||||
|
streamInstance.getTracks().forEach(track => {
|
||||||
|
track.stop();
|
||||||
|
});
|
||||||
|
streamInstance = null;
|
||||||
|
}
|
||||||
|
// 重置录音状态
|
||||||
isRecording = false;
|
isRecording = false;
|
||||||
//取消监听
|
mediaRecorderInstance = null;
|
||||||
|
audioChunks = [];
|
||||||
|
// 恢复麦克风图标
|
||||||
|
const recordIcon = document.getElementById('recordIcon');
|
||||||
|
if (recordIcon) {
|
||||||
|
recordIcon.className = 'bi bi-mic';
|
||||||
|
}
|
||||||
|
// 移除点击事件监听器
|
||||||
document.removeEventListener('click', handleMouseClick);
|
document.removeEventListener('click', handleMouseClick);
|
||||||
//取消定时器
|
|
||||||
if (recordingInterval) {
|
|
||||||
clearInterval(recordingInterval);
|
|
||||||
recordingInterval = null;
|
|
||||||
}
|
|
||||||
//变换图标
|
|
||||||
document.getElementById('recordIcon').className = 'bi bi-mic';
|
|
||||||
//发送剩下的数据
|
|
||||||
if (audio_chunks.length > 0) {
|
|
||||||
const merged_data = new Uint8Array(audio_chunks.reduce((acc, chunk) => acc + chunk.length, 0));
|
|
||||||
let offset = 0;
|
|
||||||
for (const chunk of audio_chunks) {
|
|
||||||
merged_data.set(new Uint8Array(chunk), offset);
|
|
||||||
offset += chunk.length;
|
|
||||||
}
|
|
||||||
socket.emit('VoiceMessage', merged_data, { binary: true });
|
|
||||||
audio_chunks = [];
|
|
||||||
}
|
|
||||||
overlay.style.display = 'none';
|
|
||||||
//关闭音频流
|
|
||||||
if (voice_stream) {
|
|
||||||
voice_stream.stop_stream();
|
|
||||||
voice_stream.close();
|
|
||||||
voice_stream = null;
|
|
||||||
}
|
|
||||||
if (p) {
|
|
||||||
p.terminate();
|
|
||||||
p = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//鼠标点击终止事件
|
||||||
|
function handleMouseClick(event) {
|
||||||
|
event.stopPropagation();
|
||||||
|
stopRecording();
|
||||||
|
}
|
||||||
|
function sendVoiceMessage() {
|
||||||
|
if (isRecording) {
|
||||||
|
stopRecording();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
startAudioRecording();
|
||||||
|
// 变换录音中图标
|
||||||
|
const recordIcon = document.getElementById('recordIcon');
|
||||||
|
if (recordIcon) {
|
||||||
|
recordIcon.className = 'bi bi-record-circle';
|
||||||
|
}
|
||||||
|
// 添加点击事件监听器
|
||||||
|
document.addEventListener('click', handleMouseClick);
|
||||||
|
}
|
||||||
|
//页面卸载时停止录音
|
||||||
|
window.addEventListener('beforeunload', () => {
|
||||||
|
if (isRecording) {
|
||||||
|
stopRecording();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
let currentChapterIndex = -1;
|
let currentChapterIndex = -1;
|
||||||
function next_chapter(data) {
|
function next_chapter(data) {
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ api_key = sk-aMpnWklN2IbsK44d1kNpy6YOP9bk1pdPJjFeEmbb0a5ytEFf
|
|||||||
model=gpt-4.1-nano
|
model=gpt-4.1-nano
|
||||||
|
|
||||||
[VSCODE_WEB]
|
[VSCODE_WEB]
|
||||||
url = https://hsamooc.cn
|
url = https://hsamooc.com
|
||||||
[CODE_LIKE]
|
[CODE_LIKE]
|
||||||
url = https://hsamooc.cn/vsc-like
|
url = https://hsamooc.com/vsc-like
|
||||||
#http://asengine.net:8282
|
#http://asengine.net:8282
|
||||||
[VSCODE_WEB_PATH]
|
[VSCODE_WEB_PATH]
|
||||||
is_wsl = true
|
is_wsl = true
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ api_key = sk-aMpnWklN2IbsK44d1kNpy6YOP9bk1pdPJjFeEmbb0a5ytEFf
|
|||||||
model=gpt-4.1-nano
|
model=gpt-4.1-nano
|
||||||
|
|
||||||
[VSCODE_WEB]
|
[VSCODE_WEB]
|
||||||
url = https://hsamooc.cn
|
url = https://hsamooc.com
|
||||||
#http://asengine.net:8282
|
#http://asengine.net:8282
|
||||||
[VSCODE_WEB_PATH]
|
[VSCODE_WEB_PATH]
|
||||||
is_wsl = true
|
is_wsl = true
|
||||||
@@ -43,7 +43,7 @@ dir = db/data/user/
|
|||||||
[COURSE_DATA]
|
[COURSE_DATA]
|
||||||
dir = db/data/course/
|
dir = db/data/course/
|
||||||
[MONGO]
|
[MONGO]
|
||||||
uri = mongodb://admin:12138cake@hsamooc.cn:27017/hsamooc_test?authSource=admin
|
uri = mongodb://admin:12138cake@hsamooc.com:27017/hsamooc_test?authSource=admin
|
||||||
|
|
||||||
[TENCENT_COS]
|
[TENCENT_COS]
|
||||||
secret_id = AKIDZ0EG4f2FE0YszsXzEF5h5GDwSlOtDLGx
|
secret_id = AKIDZ0EG4f2FE0YszsXzEF5h5GDwSlOtDLGx
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ function ChangeWorkspacefileTree(fileTree) {
|
|||||||
|
|
||||||
// vscode.window.showInformationMessage('Connecting to server...');
|
// vscode.window.showInformationMessage('Connecting to server...');
|
||||||
|
|
||||||
vs_socket = io('wss://hsamooc.cn/vscode');
|
vs_socket = io('wss://hsamooc.com/vscode');
|
||||||
vs_socket.on('connect', () => {
|
vs_socket.on('connect', () => {
|
||||||
// vscode.window.showInformationMessage('Connected to server');
|
// vscode.window.showInformationMessage('Connected to server');
|
||||||
console.log('Connected to server');
|
console.log('Connected to server');
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
socket = io('https://hsamooc.cn/vsc-like',{path:'/vsc-like-ws'});
|
socket = io('https://hsamooc.com/vsc-like',{path:'/vsc-like-ws'});
|
||||||
const status = document.getElementById("status")
|
const status = document.getElementById("status")
|
||||||
|
|
||||||
socket.on("pty_output", function(data){
|
socket.on("pty_output", function(data){
|
||||||
|
|||||||
Reference in New Issue
Block a user