小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!

// test20.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<string.h>
#include<deque> using namespace std; class Solution {
public:
vector<vector<int> > FindContinuousSequence(int sum) {
int half = sum / 2 + 1;
int result = 0;
vector<int> group;
vector<vector<int>> vec;
for (int time = 1;time <= half;++time)
{
for (int num = time;num < sum;++num)
{
group.push_back(num);
result += num;
if (result == sum)
{
vec.push_back(group);
group.clear();
result = 0;
break;
}
else if(result>sum)
{
group.clear();
result = 0;
break;
}
else
{ }
}
}
return vec;
}
};
int main()
{ Solution so;
vector<vector<int>> vec = so.FindContinuousSequence(1); for (auto it = vec.begin();it != vec.end();++it)
{
for (auto num = it->begin();num != it->end();++num)
{
cout << *num << " ";
}
cout << endl;
} cout << endl;
return 0;
}
上一篇:研究微信小程序


下一篇:微信公众号开发之怎样将本机IP映射成外网域名