Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 最大整数和_C++_Algorithm_C++11 - Fatal编程技术网

C++ 最大整数和

C++ 最大整数和,c++,algorithm,c++11,C++,Algorithm,C++11,编辑:解决了!我将负数测试用例视为0,而不是让输出也为负数。谢谢你的帮助 以下是挑战描述: 我一直得到一个部分解决的分数。我想知道为什么。在我看来,这个代码是有效的。我相信它是准时的。谢谢你的关注 这是我的密码: #include <iostream> #include <fstream> #include <cstdlib> #include <vector> #include <string>

编辑:解决了!我将负数测试用例视为0,而不是让输出也为负数。谢谢你的帮助

以下是挑战描述: 我一直得到一个部分解决的分数。我想知道为什么。在我看来,这个代码是有效的。我相信它是准时的。谢谢你的关注

这是我的密码:

    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    #include <string>
    #include <sstream>

    using namespace std;

    int max(int a, int b)
    {
        if (a > b)
            return a;
        else return b;
    }


    int maxSubArray(vector<int> values)
    {
        int max_so_far = values[0];
        int curr_max = values[0];
        for(int i = 1; i < values.size(); ++i)
        {
            curr_max = max(values[i], curr_max + values[i]);
            max_so_far = max(max_so_far, curr_max);
        }
        return max_so_far;
    }

    int main(int argc, char *argv[]) 
    {
        std::vector<vector<int> > Values; //to hold the values of the stock price change
        ifstream file(argv[1]);
        std::string line; //for the txt file input
        int value = 0; //for holding the value of stock change

        while (std::getline(file, line)) 
        {
            int pos = 0;
            if(line.length() == 0)
                continue;
            else
            {
                std::istringstream iss(line);
                std::vector<int> list; // temporary list of values to be pushed back into the 2-d vector
                while (iss >> value)
                {   
                    list.push_back(value);  
                }

                Values.push_back(list);
            }
        }

        for(int i = 0; i < Values.size(); ++i)
        {
            cout << maxSubArray(Values[i]);
            cout << endl;
        }
    /*  
        cout << " Printing the values : " << endl;
        for (int j = 0; j < Values.size(); ++j)
        {
            for (int k = 0; k < Values[j].size(); ++k)
                cout << Values[j][k] << " ";
            cout << endl;
        }
    */
        return 0;
    } 
为什么在这里按值传递向量

int MAXSARAYVECTOR值

这看起来是一个重要的优化机会

我认为你没有完全正确地理解这个问题。当他们说“所有相邻的次ararys”时,他们的意思是你必须取foridx=i的所有i和j的最大值;i 仅从他们提供的输出示例来看,我可以看出您的代码不会给出他们期望的答案


这似乎是对的,我唯一能想到的是,当列表变长时,您的结果可能会溢出,因此将int改为long-long。

除了在其他答案中建议的技术优化外,关于算法,我认为稍加修改可以使您的算法工作。当curr_max下降到负值时,由于遇到一个超过curr_max的负整数,您可以简单地删除所有以前的整数,包括当前值,然后重新开始。此修复非常简单,您可以向循环中添加一行,如下所示:

for(int i = 1; i < values.size(); ++i)
{
    curr_max = max(values[i], curr_max + values[i]);
    curr_max = max(curr_max, 0); // <---------------- add this line
    max_so_far = max(max_so_far, curr_max);
}

所以我现在交换了一些代码。我得到了更好的分数,但我认为这仍然是一个部分

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>

using namespace std;

int max(int a, int b)
{
    if (a > b)
        return a;
    else return b;
}


int maxSubArray(vector<int> values)
{
    int max_so_far = values[0];
    int curr_max = values[0];
    if (curr_max < 0) 
    {
        curr_max = 0;           
        max_so_far = 0;
    }
    for(int i = 1; i < values.size(); ++i)
    {
        curr_max = max(values[i], curr_max + values[i]);
        curr_max = max(curr_max, 0);
        max_so_far = max(max_so_far, curr_max);
    }
    return max_so_far;
}

int main(int argc, char *argv[]) 
{
    std::vector<vector<int> > Values; //to hold the values of the stock price change
    ifstream file(argv[1]);
    std::string line; //for the txt file input
    std::string token; //for the subtring that will be converted from char to int
    int value = 0; //for holding the value of stock change
    int count = 0;// for holding how many total cases

    while (!file.eof()) 
    {
        int pos = 0;
        getline(file, line);
        if(line.length() == 0)
            continue;
        else
        {

            std::vector<int> list; // temporary list of values to be pushed back into the 2-d vector

            while ((pos = line.find(",")) != std::string::npos )
            {
                token = line.substr(0,pos);
                value = atoi(token.c_str());
                line.erase(0, pos + 1); 
                list.push_back(value);  
            }
            value = atoi(line.c_str());
            list.push_back(value);

            Values.push_back(list);

            ++count;
        }
    }

    for(int i = 0; i < Values.size(); ++i)
    {
        cout << maxSubArray(Values[i]);
        cout << endl;
    }

    cout << " Printing the values : " << endl;
    for (int j = 0; j < Values.size(); ++j)
    {
        for (int k = 0; k < Values[j].size(); ++k)
            cout << Values[j][k] << " ";
        cout << endl;
    }

    return 0;
} 

是我,还是原始问题CodeEval 17几乎毫无意义?我甚至不能在头脑中结合样本输入来产生样本输出。@ChristopherSchultz对我来说很有意义。例如,将从2开始到5结束的序列相加,得到8;例如,第二,添加整个序列得到12。@DanielWagner我现在看到了。谢谢你的详细解释。我认为CodeEval上提出的原始问题的措辞确实很糟糕。@ChristopherSchultz就其价值而言,我完全同意它的措辞非常糟糕。这段代码失败的一个具体测试案例是:如果列表中的所有项都是负数,最大和可能应该是空的子序列,和为0,而不是一个条目的子序列与负和。你确定你的投诉在2?这看起来像是对所描述的算法的一个很小的变化。具体而言,行curr_max=maxvalues[i],curr_max+values[i];是代码中可以选择i>0的部分。@DanielWagner:我仍然认为他的代码是错误的,你发布的链接很有趣,我不知道这个技巧。但是做maxvalue[i],curr_max+values[i]而不是max0,curr_max+values[i]听起来好像不可能工作,而且速度会慢一些。我无法找出一个测试用例,而这段代码无法工作。