Exemplo n.º 1
0
int main(void){
	int n;
	int target;
	while(scanf("%d %d",&n,&target)!=EOF&&n>=4){
		int *arr=(int *)malloc(n*sizeof(int ));
		if(arr==NULL){
			exit(EXIT_FAILURE);
		}
		for(int i=0;i<n;i++){
			scanf("%d",arr+i);
		}
		int returnSize=0;
		int **result=fourSum(arr,n,target,&returnSize);
		for(int i=0;i<returnSize;i++){
			int *temp=(result[i]);
			for(int j=0;j<N;j++){
				printf("%d   ",temp[j]);
			}
			printf("\n");
		}
		
		for(int i=0;i<returnSize;i++){
			free(result[i]);
			result[i]=NULL;
		}
		
	}
	return 0;
}
Exemplo n.º 2
0
	void test() {
		vector<int> num = { -1, 0, -5, -2, -2, -4, 0, 1, -2 }; // { 1, 0, -1, 0, -2, 2 };
		auto result = fourSum(num, -9); // 0
		for (auto v : result) {
			for (int x : v) {
				cout << x << ' ';
			}
			cout << endl;
		}
	}
Exemplo n.º 3
0
    /**
     * @param numbers: Give an array numbersbers of n integer
     * @param target: you need to find four elements that's sum of target
     * @return: Find all unique quadruplets in the array which gives the sum of
     *          zero.
     */
    LLI fourSum(LI nums, int target) {
        sort(nums.begin(), nums.end());

        LLI res;
        LI set;

        fourSum(res, set, nums, 0, target);

        return res;
    }
Exemplo n.º 4
0
int main() {
    //  int arr[] = {1, 0, -1, 0, -2, 2};
    int arr[] = {
        -3,-2,-1,0,0,1,2,3
    };
    int n = sizeof arr / sizeof *arr;
    int target = 0;
    int returnSize = 0;

    int **m = fourSum(arr, n, target, &returnSize);
    for (int **pp = m; pp != m + returnSize; ++pp) {
        print(*pp, 4);
    }
    printf("return size is %d\n", returnSize);
    dealloc(m, returnSize);

    return 0;
}
Exemplo n.º 5
0
    void fourSum(LLI &res, LI &set, const LI &nums, int lo, int target) {
        int sum = 0;
        for (const int &x : set)
            sum += x;

        if (set.size() == 4 && sum == target) {
            res.push_back(set);
            return;
        }

        if (4 <= set.size())
            return;

        for (int i = lo; i < nums.size(); ++i) {
            if (i != lo && nums[i] == nums[i - 1])
                continue;

            set.push_back(nums[i]);
            fourSum(res, set, nums, i + 1, target);
            set.pop_back();
        }
    }
Exemplo n.º 6
0
                } else if (s < t) {
                    k = next(a, k, l);
                } else {
                    l--;
                }
            }
        }
    }
    *rsize = rs;
    free(a);
    return ret;
}

void try(int *nums, int size, int target) {
    int rlen;
    int **a = fourSum(nums, size, target, &rlen);
    showArr2(a, rlen, 4);
    int i;
    for (i = 0; i != rlen; i++) {
        free(a[i]);
    }
    free(a);
}

int main() {
    int a[] = {1, 0, -1, 0, -2, 2};
    try(a, COUNT_OF(a), 0);
    int b[] = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
    try(b, COUNT_OF(b), 10);
    try(b, COUNT_OF(b), 11);
    try(b, COUNT_OF(b), 12);
Exemplo n.º 7
0
int main(){
    vector<int> nums = {1, 0, -1, 0, -2, 2};
    vector<vector<int> > res = fourSum(nums, 0);
    for(auto v : res) print_vector<int>(v);
    return 0;
}
Exemplo n.º 8
0
int main() {
    fourSum({2, 2, 3, 4, 5, -1, -8}, 0);
    fourSum({1,0,-1,0,-2,2}, 0);
    return 0;
}