给定一个整数数组,判断能否从中找出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. Ubantu16.04系统优化

    系统清理篇 系统更新 安装完系统之后,需要更新一些补丁.Ctrl+Alt+T调出终端,执行一下代码: sudo apt-get update sudo apt-get upgrade 卸载libreO ...

  2. 第一届“百度杯”信息安全攻防总决赛_Upload

    题目见i春秋ctf训练营 看到fast,就想抓个包看看,以前有道题是打开链接直接来了个跳转,当然这题不是 查看返回包,发现一个好东西 拿去base64解码看看 感觉给出的字符串能继续解码,果然解码后得 ...

  3. [ZJOI2016]小星星

    题目描述 小Y是一个心灵手巧的女孩子,她喜欢手工制作一些小饰品.她有n颗小星星,用m条彩色的细线串了起来,每条细线连着两颗小星星. 有一天她发现,她的饰品被破坏了,很多细线都被拆掉了.这个饰品只剩下了 ...

  4. poj 2689 (素数二次筛选)

    Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...

  5. 2015 多校联赛 ——HDU5363(快速幂)

    Problem Description soda has a set S with n integers {1,2,…,n}. A set is called key set if the sum o ...

  6. POJ2774 很长的信息

    Description Little cat在Byterland的首都读物理专业.这些天他收到了一条悲伤地信息:他的母亲生病了.担心买火车票花钱太多(Byterland是一个巨大的国家,因此他坐火车回 ...

  7. poj 1113 凸包周长

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Descriptio ...

  8. bzoj1132[POI2008]Tro 计算几何

    1132: [POI2008]Tro Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1722  Solved: 575[Submit][Status] ...

  9. ssh远程登陆

    ssh远程登录命令简单实例   ssh命令用于远程登录上Linux主机.   常用格式:ssh [-l login_name] [-p port] [user@]hostname 更详细的可以用ssh ...

  10. idea热部署

    <!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupId> < ...