【问题标题】:Scoring function C++评分函数 C++
【发布时间】:2021-12-24 09:25:52
【问题描述】:

我正在尝试创建一个函数,将数组中的两个整数从输入的数字加到另一个整数。所以例如数组是

int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};

并且用户输入 18 和 2,因此程序会将第一个数字 (18) 中的数字相加,并在达到 2 时停止。

我尝试循环,但它一直返回错误:

进程以退出代码 139 结束(被信号 11:SIGSEGV 中断)

int cw(int first, int second ){
    int scoreCw;
    int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};

    for (int i = first; i < 20; ++i)
        if(i != second){
            scoreCw += arr[i];
        }
    return scoreCw;
}

【问题讨论】:

  • 您的scoreCW 变量已声明但未初始化。
  • 不知道为什么你会得到 SIGSEGV,但我认为你实际上想要 if(arr[i] != second) 而不是 if(i != second)else break;
  • 您是从arr[first] 开始还是从arr[n] == first 开始?
  • 另外,你怎么知道firstsecond之间有20个元素?当i == second(或者是arr[i] == second)时,您可能应该break

标签: c++ arrays sum


【解决方案1】:

你也可以这样做:

int cw(int first, int second){
    int scoreCw = 0;
    bool need_to_sum = 0;
    static const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
    for (int i =0; i<20; i++){
        if(arr[i] == first) need_to_sum = 1;
        scoreCw += (arr[i] * need_to_sum);
        if (arr[i] == second) break;
    }
    return scoreCw;
}

【讨论】:

    【解决方案2】:

    根据您描述的场景,182 不是数组索引,但代码将它们视为它们。 18 位于索引 2,2 位于索引 8。因此,从索引 0 开始,找到值为 18 的元素,然后继续循环,现在计数,直到找到值为 @ 的元素987654328@,如:

    int cw(int first, int second) {
        int scoreCw = 0;
        const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
        const size_t num = sizeof(arr)/sizeof(*arr); // or std::size(arr)
    
        for (int i = 0; i < num; ++i) {
            if (arr[i] == first) {
                do {
                    scoreCw += arr[i++];
                }
                while ((arr[i-1] != second) && (i < num));
                break;
            }
        }
    
        return scoreCw;
    }
    

    Online Demo

    或者,使用标准 C++ 算法:

    #include <algorithm>
    #include <numeric>
    #include <iterator>
    
    int cw(int first, int second) {
        const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
        auto arr_end = std::cend(arr);
        auto f = std::find(std::cbegin(arr), arr_end, first);
        auto s = std::find(f, arr_end, second);
        if (s != arr_end) ++s;
        return std::accumulate(f, s, 0);
    }
    

    Online Demo

    【讨论】:

      【解决方案3】:

      由于firstsecond 是数组中的值还是索引中的值之间存在歧义,我们假设它们是值。

      int sum_range_values(int first_value, int second_value)
      {
        int sum = 0;
        static const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
        // Find first value in the array.
        int first_index = 0;
        for (first_index = 0; first_index < sizeof(arr)/sizeof(int); ++first_index)
        {
            if (arr[first_index] == first_value)
            {
                break;
            }
        }
        if (first_index >= sizeof(arr)/sizeof(int)) return sum;
        // Sum up values until the second value is found.  
        for (int second_index = first_index; second_index < sizeof(arr)/sizeof(int); ++ second_index)
        {
            sum += arr[second_index];
            if (arr[second_index] == second_value) break;
        }
        return sum;
      }
      

      【讨论】:

        【解决方案4】:

        @Onyambu 回答的另一个转折点,希望也更简单一些。

        我已经修改了你的数组来测试我们只有在找到序列first-second(或first - 数组结尾)后才停止计数:

        • first 之前添加second2, 18, 4, 13...
        • second 之后添加first10, 15, 2, 18...
        int cw(int first, int second)
        {
            int scoreCw{0};
            int arr[] = {20, 1, 2, 18, 4, 13, 6, 10, 15, 2, 18, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
        
            bool counting{false};
            for (int n : arr)
            {
                if (n == first and not counting) { counting = true; }
                else if (n == second and counting) { break; }
        
                scoreCw += (counting ? n : 0);
            }
        
            return scoreCw;
        }
        

        Demo

        【讨论】:

          猜你喜欢
          • 2016-05-29
          • 2014-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多