<legend id='HJTaF'><style id='HJTaF'><dir id='HJTaF'><q id='HJTaF'></q></dir></style></legend>

    • <bdo id='HJTaF'></bdo><ul id='HJTaF'></ul>
    1. <small id='HJTaF'></small><noframes id='HJTaF'>

      <tfoot id='HJTaF'></tfoot>
      <i id='HJTaF'><tr id='HJTaF'><dt id='HJTaF'><q id='HJTaF'><span id='HJTaF'><b id='HJTaF'><form id='HJTaF'><ins id='HJTaF'></ins><ul id='HJTaF'></ul><sub id='HJTaF'></sub></form><legend id='HJTaF'></legend><bdo id='HJTaF'><pre id='HJTaF'><center id='HJTaF'></center></pre></bdo></b><th id='HJTaF'></th></span></q></dt></tr></i><div id='HJTaF'><tfoot id='HJTaF'></tfoot><dl id='HJTaF'><fieldset id='HJTaF'></fieldset></dl></div>

        计算存储在向量中的值的中位数 - C++?

        时间:2023-09-16
          <tfoot id='ts4kG'></tfoot>
            <tbody id='ts4kG'></tbody>

            <bdo id='ts4kG'></bdo><ul id='ts4kG'></ul>

            <small id='ts4kG'></small><noframes id='ts4kG'>

              <legend id='ts4kG'><style id='ts4kG'><dir id='ts4kG'><q id='ts4kG'></q></dir></style></legend>

                • <i id='ts4kG'><tr id='ts4kG'><dt id='ts4kG'><q id='ts4kG'><span id='ts4kG'><b id='ts4kG'><form id='ts4kG'><ins id='ts4kG'></ins><ul id='ts4kG'></ul><sub id='ts4kG'></sub></form><legend id='ts4kG'></legend><bdo id='ts4kG'><pre id='ts4kG'><center id='ts4kG'></center></pre></bdo></b><th id='ts4kG'></th></span></q></dt></tr></i><div id='ts4kG'><tfoot id='ts4kG'></tfoot><dl id='ts4kG'><fieldset id='ts4kG'></fieldset></dl></div>
                  本文介绍了计算存储在向量中的值的中位数 - C++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我是一名编程学生,对于我正在进行的一个项目,我必须做的事情之一是计算 int 值向量的中值.我将仅使用 STL 中的排序函数和向量成员函数(例如 .begin().end().size>)来执行此操作().

                  I'm a programming student, and for a project I'm working on, on of the things I have to do is compute the median value of a vector of int values. I'm to do this using only the sort function from the STL and vector member functions such as .begin(), .end(), and .size().

                  我还应该确保我找到中位数,无论向量的值是奇数还是偶数.

                  I'm also supposed to make sure I find the median whether the vector has an odd number of values or an even number of values.

                  卡住,下面是我的尝试.那么我哪里出错了?如果您愿意给我一些指示或资源以朝着正确的方向前进,我将不胜感激.

                  And I'm Stuck, below I have included my attempt. So where am I going wrong? I would appreciate if you would be willing to give me some pointers or resources to get going in the right direction.

                  代码:

                  int CalcMHWScore(const vector<int>& hWScores)
                  {
                       const int DIVISOR = 2;
                       double median;
                       sort(hWScores.begin(), hWScores.end());
                       if ((hWScores.size() % DIVISOR) == 0)
                       {
                           median = ((hWScores.begin() + hWScores.size()) + (hWScores.begin() + (hWScores.size() + 1))) / DIVISOR);
                       }
                       else 
                       {
                         median = ((hWScores.begin() + hWScores.size()) / DIVISOR)
                       }
                  
                      return median;
                  }
                  

                  推荐答案

                  你正在做一个额外的划分,总体上使它比需要的更复杂一些.此外,当 2 在上下文中实际上更有意义时,就没有必要创建 DIVISOR.

                  You are doing an extra division and overall making it a bit more complex than it needs to be. Also, there's no need to create a DIVISOR when 2 is actually more meaningful in context.

                  double CalcMHWScore(vector<int> scores)
                  {
                    size_t size = scores.size();
                  
                    if (size == 0)
                    {
                      return 0;  // Undefined, really.
                    }
                    else
                    {
                      sort(scores.begin(), scores.end());
                      if (size % 2 == 0)
                      {
                        return (scores[size / 2 - 1] + scores[size / 2]) / 2;
                      }
                      else 
                      {
                        return scores[size / 2];
                      }
                    }
                  }
                  

                  这篇关于计算存储在向量中的值的中位数 - C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:是否使用 std::vector<bool>C++ 中的对象可以接受 下一篇:多维向量

                  相关文章

                  最新文章

                    <bdo id='7Aa7U'></bdo><ul id='7Aa7U'></ul>

                  <tfoot id='7Aa7U'></tfoot>
                • <small id='7Aa7U'></small><noframes id='7Aa7U'>

                      <legend id='7Aa7U'><style id='7Aa7U'><dir id='7Aa7U'><q id='7Aa7U'></q></dir></style></legend>
                      <i id='7Aa7U'><tr id='7Aa7U'><dt id='7Aa7U'><q id='7Aa7U'><span id='7Aa7U'><b id='7Aa7U'><form id='7Aa7U'><ins id='7Aa7U'></ins><ul id='7Aa7U'></ul><sub id='7Aa7U'></sub></form><legend id='7Aa7U'></legend><bdo id='7Aa7U'><pre id='7Aa7U'><center id='7Aa7U'></center></pre></bdo></b><th id='7Aa7U'></th></span></q></dt></tr></i><div id='7Aa7U'><tfoot id='7Aa7U'></tfoot><dl id='7Aa7U'><fieldset id='7Aa7U'></fieldset></dl></div>