【查找】在按照绝对值排序的数组中,找出和为k的两个数的下标

题目:EPI


提示:

1、假设和为k的两个数,一个为非负数一个为负数,找出其下标。

2、若按照1的方法查找失败,则两个数肯定都为非负数或都为负数。


pair<int, int> one_nonnegative_one_negative(int* num, int length, int k)
{
	pair<int, int> res(length-1, length-1);//非负、负
	while (res.first >= 0 && num[res.first] < 0)
		res.first--;
	while (res.second >= 0 && num[res.second] >= 0)
		res.second--;
	while (res.first >= 0 && res.second >= 0)
	{
		if (num[res.first] + num[res.second] == k)
			return res;
		else if (num[res.first] + num[res.second] < k)//负数左移
		{
			do{
				res.second--;
			} while (res.second >= 0 && num[res.second] >= 0);
		}
		else//非负数左移
		{
			do{
				res.first--;
			} while (res.first >= 0 && num[res.first] < 0);
		}
	}
	return pair<int, int>(-1, -1);

}

pair<int, int> both_the_same_sign(int* num, int length, int k)
{
	if (k >= 0)//两个非负数
	{
		pair<int, int> res(0, length - 1);
		while (res.first <length && num[res.first] < 0)
			res.first++;
		while (res.second >= 0 && num[res.second] < 0)
			res.second--;
		while (res.first <= res.second)
		{
			if (num[res.first] + num[res.second] == k)
				return res;
			else if (num[res.first] + num[res.second] < k)
			{
				do{
					//
					res.first++;
				} while (res.first <= res.second && num[res.first] < 0);
			}
			else
			{
				do{
					res.second--;
				} while (res.first <= res.second && num[res.second] < 0);
			}
		}
		return pair<int, int>(-1, -1);

	}
	else//两个负数
	{
		pair<int, int> res(0, length - 1);
		while (res.first <length && num[res.first] >= 0)
			res.first++;
		while (res.second >= 0 && num[res.second] >= 0)
			res.second--;
		while (res.first <=res.second)
		{
			if (num[res.first] + num[res.second] == k)
				return res;
			else if (num[res.first] + num[res.second] < k)
			{
				do{
					res.second--;
				} while (res.first <= res.second && num[res.second] >= 0);
			}
			else
			{
				do{
					res.first++;
				} while (res.first <= res.second && num[res.first] >= 0);
			}
		}
		return pair<int, int>(-1, -1);
	}
}

pair<int, int> find_sum_is_k(int* num, int length, int k)
{
	pair<int, int> res(-1, -1);
	if (num == nullptr || length <= 0)
		throw new exception("error");
	res = one_nonnegative_one_negative(num, length, k);//一个非负,一个负
	if (res.first == -1 && res.second == -1)
		res = both_the_same_sign(num, length, k);//两个一样符号
	return res;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要计算每段数组相邻个数的差的绝值之和,您可以按照以下步骤进行: 1. 首先,按照上述的方法将数组分成k段。 2. 对于每一段数组,使用一个循环从0到m-2来遍历当前段中的元素。 3. 在循环迭代中,计算当前元素与下一个元素之间的差的绝对值,并累加到一个变量sum中。 4. 在最后一个元素后面没有下一个元素,因此不需要计算差值,直接跳过。 5. 输出每段数组相邻个数的差的绝对值之和。 以下是修改后的示例代码: ```cpp #include <iostream> #include <cmath> void splitArray(int arr[], int n, int k) { int m = (n % k == 0) ? (n / k) : (int)(std::ceil((double)n / k)); int** segments = new int*[k]; for (int i = 0; i < k; i++) { segments[i] = new int[m]; for (int j = 0; j < m; j++) { int index = i * m + j; if (index < n) { segments[i][j] = arr[index]; } } } // 计算每段数组相邻个数的差的绝对值之和 for (int i = 0; i < k; i++) { int sum = 0; for (int j = 0; j < m - 1; j++) { sum += std::abs(segments[i][j] - segments[i][j + 1]); } std::cout << "Segment " << i + 1 << ": " << sum << std::endl; } // 释放动态分配的内存 for (int i = 0; i < k; i++) { delete[] segments[i]; } delete[] segments; } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int n = sizeof(arr) / sizeof(arr[0]); int k = 3; splitArray(arr, n, k); return 0; } ``` 这段代码将数组`{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}`分成了3段循环,并计算了每段数组相邻个数的差的绝对值之和。输出结果如下: ``` Segment 1: 2 Segment 2: 2 Segment 3: 2 ``` 每段数组相邻个数的差的绝对值之和分别为2。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值