-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57_2_Find_Continuous_Seq.cpp
More file actions
79 lines (57 loc) · 1.47 KB
/
57_2_Find_Continuous_Seq.cpp
File metadata and controls
79 lines (57 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Created by mark on 2019/7/27.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:57_2. 和为s的连续序列。
2. 思路:类似于上个双指针方法,small和big为相邻的,累计small和big之间的数字,与sum比较。
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
using namespace std;
// 输入和,在1~n中找连续序列
void PrintContinuousSeq(int small, int big);
void FindContiousSeq(int sum)
{
if(sum < 3) return;
int small = 1, big = 2;
int mid = (sum + 1) / 2;
int cur = small + big;
while(small < mid)
{
if(cur == sum) // 找到一组相等则输出
PrintContinuousSeq(small,big);
while(cur > sum && small < mid) // 如果大于sum,则small向前移动
{
cur -= (small++);
if(cur == sum)
PrintContinuousSeq(small, big);
}
cur += (++big); // 否则向后移动继续找
}
}
// 打印两个数字之间的数
void PrintContinuousSeq(int small, int big)
{
for(int i = small; i <= big; ++i)
cout << i << " ";
cout << endl;
}
int main()
{
int sum;
cout << "输入要查找的和:";
cin >> sum;
FindContiousSeq(sum);
return 0;
}