【 Algorithm】排序数组中查找和为n的一对数和所有组合

/*************************************************
题目:11.	递增数组中查找和为n的一对数
思路:设置头尾指针,比较两者指向的数之和与sum的大小

****************************************************/
#include"stdafx.h"
#include<iostream>
using namespace std;

bool FindNums(int arr[],int len,int sum)
{
	bool notfound = true;
	if(arr == NULL || len <1)
		return notfound;
	int *pHead = arr;
	int *pLast = arr+len-1;
	while(pHead < pLast)
	{
		long long cursum = *pHead + *pLast;
		if(cursum == sum)
		{
			notfound =true;
			cout << *pHead <<" "<<*pLast <<endl;
			break;
		}
		else if(cursum < sum)
		{
			pHead++;
		}
		else
			pLast--;
	}
	return notfound;
}
int main()
{
	int arr[8] = {1,3,4,5,6,8,11,13};
	int sum = 18;
	 cout << FindNums(arr,8,18) << endl;
	 system("pause");
	 return 0;

}
</pre><pre name="code" class="cpp">/******************************数组——背包问题*************************************** 
	题目:求数组和为定值n的m个数
	思路:典型的背包问题
	 采用
**********************************************************************/  
#include"stdafx.h"
#include<iostream>
#include<assert.h>
#include<list>
using namespace std;

list<int> ilist;

void Find_factor(int sum,int n)
{
	//递归出口
	if(sum <= 0|| n <= 0)
		return ;
	if(sum == n)//要放的数刚好是满足条件的最后一个数
	{
		for(list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
			cout << *iter << "+";
		cout << n <<endl;
	}
	ilist.push_back(n);
	Find_factor(sum-n,n);
	ilist.pop_back();
	Find_factor(sum,n-1);
}


int main()
{
	int sum, n;
	cout << "请输入你要等于多少的数值 sum:" << endl;
	cin >> sum;
	cout << "请输入你要从 1.....n 数列中取值的 n:" << endl;
	cin >> n;
	cout << "所有可能的序列,如下:" << endl;
	Find_factor(sum,n);

	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用类模板对数组进行排序查找元素和的程序的示例: ```cpp #include <iostream> #include <algorithm> using namespace std; template <typename T, int size> class Array { private: T data[size]; public: // 构造函数 Array() {} // 添加元素到数组 void add(T value, int index) { data[index] = value; } // 对数组进行排序 void sort() { std::sort(data, data + size); } // 查找元素 int find(T value) { for (int i = 0; i < size; i++) { if (data[i] == value) return i; } return -1; } // 元素和 T sum() { T sum = 0; for (int i = 0; i < size; i++) { sum += data[i]; } return sum; } // 输出数组 void print() { for (int i = 0; i < size; i++) { cout << data[i] << " "; } cout << endl; } }; int main() { Array<int, 5> arr; // 添加元素到数组 arr.add(5, 0); arr.add(2, 1); arr.add(9, 2); arr.add(1, 3); arr.add(3, 4); // 输出原始数组 cout << "Original array: "; arr.print(); // 排序数组 arr.sort(); // 输出排序后的数组 cout << "Sorted array: "; arr.print(); // 查找元素 int index = arr.find(9); if (index == -1) { cout << "Element not found" << endl; } else { cout << "Element found at index " << index << endl; } // 元素和 int sum = arr.sum(); cout << "Sum of elements: " << sum << endl; return 0; } ``` 这个程序使用了类模板 `Array`,它有一个私有成员 `data`,用于存储数组元素。程序提供了四个公有成员函数: - `add`:添加元素到数组。 - `sort`:对数组进行排序。 - `find`:查找元素在数组中的位置。 - `sum`:数组元素的和。 程序还提供了一个 `print` 函数,用于输出数组元素。 在 `main` 函数中,我们首先创建了一个 `Array` 对象,并使用 `add` 函数添加了五个元素。然后,我们输出了原始数组,对数组进行排序,输出排序后的数组,查找数组中的元素 9 的位置,数组元素的和,并输出结果。 运行程序,输出如下: ``` Original array: 5 2 9 1 3 Sorted array: 1 2 3 5 9 Element found at index 4 Sum of elements: 20 ``` 希望这个示例程序对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值