一个长度为N的数组中包含正数 负数 0,请实现一个函数找出和为0的最长子数列

//

#include
#include
using namespace std;
void ZeroLongSet(vector &v)
{
	if (v.empty())
		return;
	int maxdif = 0;
	int temp=0,temp2;
	int start=0, end=0;
	for (size_t i = 0; i != v.size(); ++i)
	{
		temp2 = temp;
		for (size_t j = i; j != v.size(); ++j)
		{
			temp2 = temp2 + v[j];
			if (temp2 == temp)
			{
				if (j - i > maxdif)
				{
					maxdif = j - i;
					start = i ;
					end = j;
				}				
			}
		}
		temp = temp + v[i];
	}	
	if (end != 0)
	{
		for (size_t k = start; k != end+1; ++k)
			cout << v[k] << " ";
	}
}
int main(int argc, char *argv[])
{
	vector v;
	int value;
	while (cin >> value)
	{
		v.push_back(value);
	}
	ZeroLongSet(v);
	system("pause");
	return 0;
}

你可能感兴趣的:(c++,primer,5th,练习题)