【问题标题】:Predefined range of variables task预定义的变量范围任务
【发布时间】:2026-02-03 09:25:01
【问题描述】:

谁能解释一下这个任务: 编写一个函数,该函数的调用将打印预定义变量范围的方程的解。该函数应该有两个参数,范围的开始和结束,寻求解的方程是: x ^ 2 + 2x - 5 x的测试函数运算属于[0.5]。

【问题讨论】:

    标签: c++ function equation


    【解决方案1】:

    我猜他们期待这样的事情:

    void solve(std::pair<float*, float*> range, const std::vector<float>& polynomCoeffs) {
      for (auto it = range.first; it != range.second; ++it) {
        // solve and std::cout the thing
      }
    }
    

    这样称呼它

    float values[] = { 0.f, 42.f, -100.f };
    std::vector<float> coeffs = { 1, 2, -5 };
    
    solve(std::make_pair(std::begin(values), std::end(values)), coeffs);
    

    【讨论】:

    • 以及等式 x ^ 2 + 2x - 5 究竟在哪里使用?
    • 在 for 循环中。你有你的 x (*it) 和你的 a/b/c (polynomCoeffs[0..2]) 。只需“求解 x”并打印出结果。
    • 如何测试 x=0.5 这个函数,我应该打印什么?抱歉,我不清楚……
    • 我读到“x 属于 [0.5]”,因为 x = { 0, 1, 2, 3, 4, 5 } 但也许我错了......
    • 范围为 1 个元素有什么意义?