方法1:数学归纳法
/*
x n
x x+1 n
x + x + k - 1 == >kx + (k-1)k/2 n
*/
int squeSumN(int n)
{
//表示几个连续的数
int count = 0;
if (n <=0)
return 0;
for (int k = 1; k < n; k++)
{
int sum = ((k - 1)*k) / 2;
int x = (n - sum) / k;
if (n - sum < 0)
break;
if (x!=0&&k*x==n-sum)
{
cout << x << "\t" << k << endl;
count++;
}
}
return count;
}
方法2:
int seq(int n)
{
int small = 1;
int big = 2;
int count = 0;
while (small <= n/2)
{
int sum = (big - small + 1)*(big + small) / 2;
if (sum == n)
{
cout << small << "\t" << big << endl;
count++;
small = small + 1;
big = small + 1;
}
if (sum < n)
big++;
if (sum > n)
small++;
}
cout << n << endl;
return count++;
}