与这个问题所说的相反,这段代码表现出一些奇怪的行为:
long long int fibonacci(int num) {
if (num <= 2) return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}
int main() {
auto t1 = std::chrono::high_resolution_clock::now();
long long int x = fibonacci(45);
auto t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> time(t2 - t1);
std::cout << "Time taken: " << time.count() << "ms";
}
Run Code Online (Sandbox Code Playgroud)
在我的机器上,使用 (GCC) 编译大约需要 700 毫秒-O3,输出为:
Time taken: 2667.55ms
Run Code Online (Sandbox Code Playgroud)
我将上面的代码重写constexpr如下:
constexpr long long int fibonacci(int num) {
if (num <= 2) …Run Code Online (Sandbox Code Playgroud) 我编写了以下JavaScipt代码,它将二进制数转换为十进制数:
(function bin_dec(num) {
var x = num;
var result = 0;
for (var i = 0; i < x.length; i++) {
result += eval(x[x.length - i] * 2^i);
}
return result;
})()
Run Code Online (Sandbox Code Playgroud)
我希望能够从命令行运行此代码.文件名是converter.js,我在与文件相同的目录中运行命令提示符窗口.我试图使用01001100函数参数运行此代码.以下是我的尝试:
$ converter.js 01001100
Run Code Online (Sandbox Code Playgroud)
和
$ converter.js -01001100
Run Code Online (Sandbox Code Playgroud)
和
$ converter.js bin_dec(01001100)
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,这些都不奏效.有人可以指出我的错误吗?提前致谢.
我是服务器发送事件的新手,所以我正在WAMP 服务器上尝试这个 W3Schools 示例。我拥有的文件是:
演示_sse.php
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
Run Code Online (Sandbox Code Playgroud)
索引.php
<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>
<script>
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
据我了解,时间在不断变化,因此必须每秒发送一次更新(至少)。但是,每三秒接收一次更新。这个间隔没有在 demo_sse.php 中指定,所以:
出于某种原因,下面操作的 doubleArray 没有显示在控制台中。在这两种情况下,我在 for 循环之后声明的任何变量都不会显示在控制台上。考虑到在第一个算法中,只有一个 for 循环,每次都递增 x。而在第二种算法中,它是一个嵌套的 for 循环。有人可以帮我解决这两种算法中的错误吗?第一种算法:
var isDuplicate = function() {
var helloWorld = [1,2,3,4,3];
var doubleValue = [];
var x = 0;
for (i = 0; i < helloWorld.length; i++) {
x = x + 1;
if (helloWorld[i] === helloWorld[x] && i !== x) {
doubleValue.push(helloWorld[i])
console.log(helloWorld[i]);
} else {
continue;
}
}
console.log(doubleValue);
};
Run Code Online (Sandbox Code Playgroud)
算法二:
var isDuplicate = function() {
var helloWorld = [1,2,3,4,3];
var doubleValue = [];
for (i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我正在模拟卡片上的翻转效果。该卡片是一个 div,里面有图像。我想在动画正好处于中点时(即当卡正好旋转 90 度时)更改该图像。过渡的计时功能设置为缓出。
所以我的问题是,缓出动画在设定的过渡持续时间的哪一部分达到中点?
我有两个 div,如下所示:
<div class="container"></div>
<div class="container"></div>
Run Code Online (Sandbox Code Playgroud)
我使用这两个 div 创建了一个数组:
var containers = document.querySelectorAll(".container");
Run Code Online (Sandbox Code Playgroud)
我还创建了一个变量来表示第一个 div:
var div1 = document.querySelectorAll(".container")[0];
Run Code Online (Sandbox Code Playgroud)
当我使用indexOf()获取数组中第一个 div 的索引时,出现错误:
Containers.indexOf 不是一个函数
这是总结我的问题的片段:
<div class="container"></div>
<div class="container"></div>
Run Code Online (Sandbox Code Playgroud)
var containers = document.querySelectorAll(".container");
Run Code Online (Sandbox Code Playgroud)
我有一个存储评论的文件.文件名是comments.xml:
<?xml version="1.0" encoding="utf-8"?>
<comment>
<user>User4251</user>
<date>02.10.2018</date>
<text>Comment body goes here</text>
</comment>
<comment>
<user>User8650</user>
<date>01.10.2018</date>
<text>Comment body goes here</text>
</comment>
Run Code Online (Sandbox Code Playgroud)
为了遍历XML树,我使用W3Schools给出的示例(对参数进行了一些修改).代码包含在index.php中:
<?php
$xml = simplexml_load_file("comments.xml") or die("Error: Cannot create object");
foreach($xml -> children() as $comments) {
echo $comments -> user . ", ";
echo $comments -> date . ", ";
echo $comments -> text . "<br>";
}
?>
Run Code Online (Sandbox Code Playgroud)
根据这个例子,我期待:
User4251, 02.10.2018, Comment body goes here
User8650, 02.10.2018, Comment body goes here
Run Code Online (Sandbox Code Playgroud)
但是,我收到三个错误:
警告: simplexml_load_file():comments.xml:7:解析器错误:第 …
我已经阅读了有关如何在C ++中创建简单窗口的Microsoft页面。这是代码:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) {
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text …Run Code Online (Sandbox Code Playgroud) 假设我定义了两个变量x和y:
int x = 5;
int y = 6;
Run Code Online (Sandbox Code Playgroud)
我可以x通过以下方式获取存储地址:
cout << &x << endl; // 0x61fe18
Run Code Online (Sandbox Code Playgroud)
这是一个十六进制数。现在,如果我想在加上 5 后打印这个数字,这很简单:
cout << &x + 5 << endl; // 0x61fe1d
Run Code Online (Sandbox Code Playgroud)
但是,如果我总结的地址一起x和y:
cout << &x + &y << endl;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Run Code Online (Sandbox Code Playgroud)invalid operands of types 'int*' and 'int*' to binary 'operator+' cout << &x + &y << endl; ~~~^~~~
为什么我不能添加这两个地址?
我有一个二进制文件,我想一次处理一个字节。这是我读取文件第一个字符的内容:
ifstream file("input.dat", ios::binary);
unsigned char c;
file >> c;
Run Code Online (Sandbox Code Playgroud)
但是,当我使用调试器单步执行此代码时,尽管文件的第一个(也是唯一的)字符是 ,但c始终具有该值。事实上,任何其他角色也被完全忽略。0x000x0A
如何从此文件中读取各个字节?