【问题标题】:Iterations getting skipped in a for loop在 for 循环中跳过迭代
【发布时间】:2021-11-15 06:32:12
【问题描述】:

我目前正在研究一种可靠性设计算法,我在此视频https://www.youtube.com/watch?v=uJOmqBwENB8 上找到了该算法。我用 C++ 编写了代码,但是我遇到了一个问题,在其中一个循环中,一些迭代被跳过了。这是整个代码:

#include <iostream>
#include <cmath>
#include <vector>
#include <utility>

using namespace std;

int main(){
    int n;
    cin>>n;
    int cost[n],num_available[n],max_cost;
    double rel[n];
    for(int i=0;i<n;i++)
        cin>>cost[i];
    for(int i=0;i<n;i++)
        cin>>rel[i];
    for(int i=0;i<n;i++)
        cin>>num_available[i];
    cin>>max_cost;

    vector<pair<double,int>> resSet;
    resSet.push_back(make_pair(1,0));

    for(int i=0;i<n;i++){
        vector<pair<double,int>> tempSet;
        for(int j=0;j<num_available[i];j++){
            for(int pos=0;pos<resSet.size();pos++){
                int sum_left_costs=0;
                for(int k=i+1;k<n;k++)
                    sum_left_costs+=cost[k];
                if(sum_left_costs+resSet[pos].second+cost[i]*(j+1)>max_cost)
                    break;
                tempSet.push_back
                    (make_pair(resSet[pos].first*(1-pow((1-rel[i]),j+1))
                            ,resSet[pos].second+cost[i]*(j+1)));
            }
        }
        for(int i=0;i<tempSet.size();i++){
            cout<<"Reliability: "<<tempSet[i].first<<
                ", Price: "<<tempSet[i].second<<endl;
        }
        resSet=tempSet;
        cout<<endl;
    }

    double maxRel=0,pos;
    for(int i=0;i<resSet.size();i++){
        if(maxRel<resSet[i].first){
            pos=i;
            maxRel=resSet[i].first;
        }
    }

    cout<<"Best Reliability: "<<resSet[pos].first<<
        " for price: "<<resSet[pos].second;

    return 0;
}

无论如何,在试图找出问题的同时,我在循环中添加了一些代码更改,只是为了看看循环经过了多少次迭代:

for(int i=0;i<n;i++){
        cout<<"i="<<i<<", ";
        vector<pair<double,int>> tempSet;
        for(int j=0;j<num_available[i];j++){
            cout<<"j="<<j<<", ";
            for(int pos=0;pos<resSet.size();pos++){
                cout<<"pos="<<pos<<", ";
                int sum_left_costs=0;
                for(int k=i+1;k<n;k++)
                    sum_left_costs+=cost[k];
                if(sum_left_costs+resSet[pos].second+cost[i]*(j+1)>max_cost)
                    break;
                tempSet.push_back
                    (make_pair(resSet[pos].first*(1-pow((1-rel[i]),j+1))
                            ,resSet[pos].second+cost[i]*(j+1)));
            }
        }
        resSet=tempSet;
        cout<<endl;
    }

现在,一旦我执行了程序,结果如下:

Input:
3
30 15 20
0.9 0.8 0.5
2 3 3
105

Output:
i=0, j=0, pos=0, j=1, pos=0, 
i=1, j=0, pos=0, pos=1, j=1, pos=0, pos=1, j=2, pos=0, pos=1, 
i=2, j=0, pos=0, pos=1, pos=2, pos=3, j=1, pos=0, pos=1, j=2, pos=0, pos=1, 
Best Reliability: 0.63 for price: 105

但预期的输出应该是:

i=0, j=0, pos=0, j=1, pos=0, 
i=1, j=0, pos=0, pos=1, j=1, pos=0, pos=1, j=2, pos=0, pos=1, 
i=2, j=0, pos=0, pos=1, pos=2, pos=3, j=1, pos=0, pos=1,pos=2,pos=3, j=2, pos=0, pos=1, pos=2, pos=3
Best Reliability: 0.648 for price: 100
Any ideas what could cause these problems?
P.S. Yes i know I need to learn how to use a debugger.

【问题讨论】:

  • 你期望得到的输出是什么?
  • 是的,我知道我需要学习如何使用调试器。越早开始,您越早获得好处。
  • 仅供参考,可变长度数组,例如 int cost[n] 不是标准 C++。我建议您将它们替换为std::vector
  • 这可能是正确的,但乍一看,我希望这里是 j 而不是 i:for(int k=i+1;k
  • 它可能会碰到你的break 语句并跳出for(int pos=0;pos&lt;resSet.size();pos++) 循环。当然,我真的无法说出预期的结果

标签: c++ algorithm for-loop


【解决方案1】:

如果您将break 切换为continue,它将获得您的预期结果。

Reliability: 0.9, Price: 30
Reliability: 0.99, Price: 60

Reliability: 0.72, Price: 45
Reliability: 0.792, Price: 75
Reliability: 0.864, Price: 60
Reliability: 0.8928, Price: 75

Reliability: 0.36, Price: 65
Reliability: 0.396, Price: 95
Reliability: 0.432, Price: 80
Reliability: 0.4464, Price: 95
Reliability: 0.54, Price: 85
Reliability: 0.648, Price: 100
Reliability: 0.63, Price: 105

Best Reliability: 0.648 for price: 100

break 将退出最内层循环,continue 将进入下一次迭代

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2013-07-24
    相关资源
    最近更新 更多