//和为s的两个数字 VS 和为s的连续正数序列
void TwoNumSum(int num[],int s,int len)
{
int i, j;
for (i = 0; i < len; i++)
{
for (j = i + 1; j < len; j++)
{
if (num[i] + num[j] == s)
{
cout << num[i] << " " << num[j] << endl;
break;
}
}
}
}
void TwoNumSum1(int num[], int s,int len)
{
int i, j, temp;
if (num == NULL||len<0)
{
return;
}
i = 0;
j = len - 1;
while (i < j)
{
temp = num[i] + num[j];
if (temp>s)
{
j--;
}
else if (temp < s)
{
i++;
}
else
{
cout << num[i] << " " << num[j] << endl;
i++;
}
}
}
void SumS(int s)
{
int first, end,i,temp;
if (s <= 0)
return;
first = 1; end = 2;
while (first < end && end <= ((1 + s) / 2))
{
temp = 0;
for (i = first; i <= end; i++)
{
temp += i;
}
if (temp < s)
{
end++;
}
else if (temp>s)
{
first++;
}
else if (temp==s)
{
cout << first << "---" << end << endl;
first++;
}
else
{
cout << "Dont have!" << endl;
}
}
}
和为s的两个数字 VS 和为s的连续正数序列
最新推荐文章于 2020-11-03 00:00:43 发布