输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。

#define N 15

void findSubquence(int n) {
	int end = (n + 1) / 2;
	int small = 1;
	int big = 2;
	int sum = small + big;
	//N/2<=small<big,small>N/2,否则,small+big>N
	while (small < end) {
		if (sum < n) {
			big++;
			sum += big;
			continue;
		} else if (sum == n) {
			cout << small << "-" << big << endl;
		}
		sum -= small;
		small++;
	}
}
05-01 03:27