简体   繁体   English

如何摆脱固定的哨兵值(-32767)?

[英]How can I get rid of fixed sentinel value (-32767)?

Below is the program I wrote to find sum of a subarray from given array, however somehow I am not getting how can I get rid of the sentinel value (-32767 in this case)? 下面是我编写的程序,用于查找给定数组的子数组的总和,但不知怎的,我不知道如何摆脱sentinel值(在这种情况下为-32767)? and how can I optimise it? 我该如何优化它? and how can I keep track of range of max subarray? 如何跟踪最大子阵列的范围?

#define EVALUE -32767

using namespace std;

int findMaxSubArray(vector<int>,int low,int high);
int findMaxSubArray_Mid(vector<int>,int low,int high);

int main()
{
    vector<int> v;
    int j=0;

    cout << "Enter array values(-32767 to end): ";
    while(1)
    {
        cin >> j;
        if (EVALUE==j)
            break;
        v.push_back(j);
    }

if(v.size()!=0)
    cout << "Max sum is: " << findMaxSubArray(v,0,v.size()-1) << "\n";
else
    cout << "No array elements entered, exiting...\n";

system("pause");
return 0;
}

int findMaxSubArray(vector<int> v, int low, int high)
{
    if(low==high) return v[low];

    int max_mid_sum=findMaxSubArray_Mid(v,low,high);
    int max_left_sum=findMaxSubArray(v,low,(low+high)/2);
    int max_right_sum=findMaxSubArray(v,(low+high)/2+1,high);

    if (max_mid_sum>max_left_sum) return (max_mid_sum>max_right_sum?max_mid_sum:max_right_sum);
    else return(max_left_sum>max_right_sum?max_left_sum:max_right_sum);
}

int findMaxSubArray_Mid(vector<int> v,int low,int high)
{
    int mid=high/2;
    int max_left_sum=0;
    int max_right_sum=0;
    int sum=0;

    for(int i=mid;i>=low;--i)
    {
        sum+=v[i];
        if(sum>max_left_sum)    
        {
            max_left_sum=sum;
        }
    }

    sum=0;
    for(int i=mid+1;i<=high;++i)
    {
        sum+=v[i];
        if(sum>max_right_sum)   
        {
            max_right_sum=sum;
        }
    }

    return (max_right_sum+max_left_sum);
}

When reading from a textfile, the last character that cin will get is the "EOF" character, or end of file character. 从文本文件中读取时,cin将获得的最后一个字符是“EOF”字符或文件结尾字符。 You can send this character to your program in the command line with control+d. 您可以使用control + d在命令行中将此字符发送到您的程序。 You're going to want to check for this rather than -32767. 你想要检查这个而不是-32767。

This is a basic program that should identify a simple fix for your problem: 这是一个基本程序,应该为您的问题找出一个简单的解决方案:

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> v;
    int j;

    cout << "Enter array values (Control+D (EOF) to end): ";
    cin >> j;
    while(cin.good())
    {
        v.push_back(j);
        cin >> j;
    }

    return 0;
}

If you want to get really smart you can use the below and it will directly insert the contents of the memory at cin (from the beginning until EOF) into your vector. 如果你想变得非常聪明,你可以使用下面的内容,它将直接将内存的内容(从开头直到EOF)插入到向量中。 As far as running time goes, this will probably be faster than your solution and the above solution. 就运行时间而言,这可能比您的解决方案和上述解决方案更快。

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    vector<int> v;
    cout << "Enter array values (Control+D (EOF) to end): ";

    istream_iterator<int> in(cin);
    istream_iterator<int> eof;
    copy(in, eof, back_inserter(v));

    ostream_iterator<int> out(cout, "\n");
    copy(v.begin(), v.end(), out);

    return 0;
}

About the sentinel, IIRC with Control+D you close standard input(may depend of OS). 关于哨兵,IIRC与Control + D你关闭标准输入(可能取决于操作系统)。 That will cause the << to fail (I am not sure how, probably you'll have to catch an exception). 这将导致<<失败(我不知道如何,可能你必须捕获异常)。

Anyway, the rest of the code is just a recursive (binomial) adding of the vector. 无论如何,其余的代码只是向量的递归(二项式)添加。 You can sustitute all of it with a simple for 你可以用一个简单的替换所有这些

for(int i = 0; i < v.size(); i++) {
  total += v[i]
}

The question about range of max subarray is already managed by the class Vector 关于max子阵列范围的问题已经由Vector类管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM