2.7.3

Posted on 2016-12-30 00:21  GaryWant  阅读(147)  评论(0)    收藏  举报
#include<iostream>
#include"array.h"
using namespace std;
#define SUCCESS 1
#define FAIL 0
static int result = 0;

int find(int *a , int p , int q , int r , int x)
{
	int n1 = q - p + 1;
	int n2 = r - q;
	int *b = (int *)malloc(sizeof(int)*n1);
	copyArray(a , b , p , q);
	int *c = (int *)malloc(sizeof(int)*n2);
	copyArray(a , c , q + 1 , r);
	for(int i = 0 ; i < n1 ; i++ )
	{
		if(indexOf(c ,n2 , x - b[i]) != -1)
			return SUCCESS;
	}
	return FAIL;
}

int findDivition(int *a , int p , int q , int x)
{
	if(p < q)
	{
		int r = (p + q)>>1;
		findDivition(a , p , r , x);
		findDivition(a , r + 1 , q , x);
		return result += find(a , p , r , q , x);
	}
}

int main()
{
	int a[10] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10};
	cout<<findDivition(a , 0 , 9 , 20);
	system("pause");
}

  

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3