给定一个整数数组,判断能否从中找出4个数a、b、c、d,使得他们的和为0,如果能,请找出所有满足和为0个4个数对。

#define SIZE 10
void judgeAndPut(int* arr, int fix1, int fix2, int begin, int end) {
	while (begin < end) {
		int sum = arr[begin] + arr[end] + arr[fix1] + arr[fix2];
		if (sum > 0) {
			end--;
		} else if (sum < 0) {
			begin++;
		} else {
			cout << " " << arr[fix1] << " " << arr[fix2] << " " << arr[begin]
					<< " " << arr[end] << endl;
			begin++;
			end--;
			while (begin + 1 < end && arr[begin + 1] == arr[begin]) {
				begin++;
			}
			while (end - 1 > begin && arr[end - 1] == arr[end]) {
				end--;
			}
		}
	}
}

void findFourSumEq0(int* arr) {
	qsort(arr, SIZE, sizeof(int), myCmp);

	for (int i = 0; i < SIZE; i++) {
		cout << " " << arr[i];
	}
	cout << endl;
	for (int i = 0; i < SIZE - 3; ++i) {
		if (i != 0 && arr[i] == arr[i - 1]) {
			continue;
		}
		for (int j = i + 1; j < SIZE - 2; ++j) {
			if (j != 1 && arr[j] == arr[j - 1]) {
				continue;
			}
			judgeAndPut(arr, i, j, j + 1, SIZE - 1);
		}
	}
}

4-sum问题的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. javascript的基础(1)

    1.javascript是什么? 它是一门基于客户端的脚本语言,是相对于服务器而言,浏览器就是一个客户端软件,浏览器从服务器上将资源(html,css,js,图片等)请求下来 并且在本地利用浏览器去解 ...

  2. [SPOJ705]不同的子串

    题目描述] 给定一个字符串,计算其不同的子串个数. [输入格式] 一行一个仅包含大写字母的字符串,长度<=50000 [输出格式] 一行一个正整数,即不同的子串个数. [样例输入] ABABA ...

  3. k-d树模板(BZOJ2648)

    实现了插入一个点,查询距某个位置的最近点. #include <cstdio> #include <algorithm> using namespace std; , inf ...

  4. c语言3种链接属性: 外部(external), 内部(internal),无设置(none)

    c语言中,多个文件组合的时候,有可能标示名相同,那么这个时候编译器如何判别的呢?    c语言中有3种链接属性: 外部(external), 内部(internal),无设置(none)    外部( ...

  5. Delphi7通过SendMessage来实现默认打印机的切换

    具体代码 procedure SetDefaultPrinter(NewDefPrinter: string); var ResStr: array[0..255] of Char; begin St ...

  6. postman 模拟请求中添加 header,post请求中传json参数

    1. GET 请求 2.Post 请求 (请求参数为Json,header中带有参数) 问题延伸 GET请求不能够 添加 Body 吗?[答案]

  7. Golang学习笔记:goroutine

    1.goroutine goroutine是go语言的并发体.在go语言里面能使用go关键字来实现并发. go func() 1.1 概念介绍 goroutine本质上是协程,我刚刚学习的时候就粗略地 ...

  8. json转化为对象数组

    1.ascx传值给aspx aspx页面 <%@ Page Title="" Language="C#" MasterPageFile="~/_ ...

  9. sublime text3中设置Emmet输入标签自动闭合

    项目后端前一段时间从C#转成了JAVA,在开发的过程中,由于HTML对标签的语法很宽松,比如这样:<img src="" alt="">在标签的结尾 ...

  10. 函数的属性和方法之call、apply 及bind

    一.前言 ECMAScript中的函数是对象,因此函数也有属性和方法.每个函数都包含两个属性:length和prototype.每个函数也包含两个非继承来的方法:apply()和call(),还有一些 ...