【问题标题】:C++ - Find numbers between first and last negative numbers in ArrayC++ - 查找数组中第一个和最后一个负数之间的数字
【发布时间】:2018-04-22 07:51:30
【问题描述】:

我收到了关于在数组中查找第一个和最后一个负数之间的数字摘要的问题,这是我的代码:

#include <iostream>
using namespace std;

int main(){
float a[] = { 1.5, 55, 5, 4.4,7.1,9,8,2,4.2, -3, -3.2, 5.2, -9 };
float eq = 0;
float sumofnums = 0;

for (int g = 0; g < 13; g++) {

    if (a[g] < 0) {
        sumofnums = a[g];
        cout << sumofnums << endl;
    }
}

我想在第一个和最后一个负数之间找到摘要,我尝试了方法: sumofnums+= a[g];但答案是 -15.2 而实际上是 -10。

【问题讨论】:

  • Yeah, I tried and the answer isn't right. 你忘了告诉我们特定输入的输出和预期输出。
  • 你的问题有点不清楚。所以在你的例子中你想总结-3, -3.2, 5.2 and -9?请edit您的问题并确认和/或澄清。
  • @user8141203 找到第一个和最后一个负数的索引:然后for(i=begin; i&lt;=end; i++) sum+= a[i];
  • @user8141203 使用通用逻辑:1)迭代数组直到找到第一个负数(存储索引) 2)反向迭代数组直到找到第一个负数(存储索引) 3)迭代从 index1 到 index2 并求和。
  • "无法通过程序找到它?"我刚刚告诉你一种以编程方式找到它的方法。

标签: c++ arrays algorithm iterator


【解决方案1】:

一种可能的解决方案:

#include <iostream>
using namespace std;

int main() {
  float a[] = { 1.5, 55, 5, 4.4,7.1,9,8,2,4.2, -3, -3.2, 5.2, -9 };
  float sumofnums = 0;

  int size = sizeof(a) / sizeof(a[0]);  // size is 13 here, this allows you
                                        // to determine the size automatically
  int first = 0;
  int last = size - 1;

  for (int g = 0; g < size; g++) {
    if (a[g] < 0) {
      first = g;
      break;
    }     
  }

  for (int g = size - 1; g >= 0; g++) {
    if (a[g] < 0) {
      last = g;
      break;
    }
  }

  for (int g = first; g <= last; g++) {
    sumofnums += a[g];
  }

  cout << "Sum is " << sumofnums << endl;
}

不用cmets应该就够清楚了。

【讨论】:

    【解决方案2】:

    这个循环

    for (int g = 0; g < 13; g++) {
    
        if (a[g] < 0) {
            sumofnums = a[g];
            cout << sumofnums << endl;
        }
    }
    

    依次将数组的负值赋给变量sumofnums,而不是累加所需范围内的所有值。

    如果使用标准算法,那么程序可能看起来像

    #include <iostream>
    #include <iterator>
    #include <algorithm>
    #include <numeric>
    #include <functional>
    
    int main()
    {
        float a[] = { 1.5, 55, 5, 4.4, 7.1, 9, 8, 2, 4.2, -3, -3.2, 5.2, -9 };
        float sumofnums = 0.0f;
    
        auto first = std::find_if(std::begin(a), std::end(a),
            std::bind2nd(std::less<float>(), 0.0f));
    
        if (first != std::end(a))
        {
            auto last = std::find_if(std::rbegin(a), std::rend(a),
                std::bind2nd(std::less<float>(), 0.0f)).base();
    
            sumofnums = std::accumulate(first, last, sumofnums);
        }
    
        std::cout << sumofnums << std::endl;
    
        return 0;
    }
    

    它的输出是

    -10
    

    如果使用手动编写的循环,那么程序可能看起来像

    #include <iostream>
    
    int main()
    {
        float a[] = { 1.5, 55, 5, 4.4, 7.1, 9, 8, 2, 4.2, -3, -3.2, 5.2, -9 };
        const size_t N = sizeof(a) / sizeof(*a);
        float sumofnums = 0.0f;
    
        const float *first = a;
    
        while (not (first == a + N) and not (*first < 0.0f))
        {
            ++first;
        }
    
        if (first != a + N )
        {
            const float *last = a + N;
    
            while (not (last == a) and not (*(last - 1 ) < 0.0f))
            {
                --last;
            }
    
            for (; first != last; ++first) sumofnums += *first;
        }
    
        std::cout << sumofnums << std::endl;
    
        return 0;
    }
    

    输出与上图相同。

    【讨论】:

    • @MichaelWalz 没有人知道这一点。:)
    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多