给定一个整数数组,判断能否从中找出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. [Codeforces 863E]Turn Off The TV

    Description Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be worki ...

  2. [Luogu 3901]Difference

    Description Input Output Sample Input 4 2 1 2 3 2 1 3 2 4 Sample Output Yes No HINT 题解 莫队.加个标记数组维护该数 ...

  3. [AHOI2009]中国象棋

    题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法.大家肯定很清楚,在中国象棋中炮的行走方式是 ...

  4. reserve的使用

    reserve: 强迫容器将它的容量变成n 可以避免不必要的重新分配 如果n大于当前容量,那么正常. 如果n小于当前容量,vector会忽略,string则是减小为 max(size(),n). 如果 ...

  5. 【HDU 2966 k-dimensional Tree 入个门】

    ·“k-d树是一种分割k维数据空间的数据结构.主要应用于多维空间关键数据的范围搜索和最近邻搜索……”’'   ·英文题,述大意:      给出平面内n个点(n<=100000,0<=x, ...

  6. bzoj2096[Poi2010]Pilots 单调队列

    2096: [Poi2010]Pilots Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 983  Solved: 513[Submit][Statu ...

  7. Java面试题—初级(8)

    基本表结构:  student(sno,sname,sage,ssex)学生表        course(cno,cname,tno) 课程表        sc(sno,cno,score) 成绩 ...

  8. C# 导入excel报错 :不是预期外部表

    错误原因:由于Excel 97-2003的连接格式与Excel 2010 的 不同造成. 解决方案1: 很多人换了2010后,问的最多的问题之一是2003里最经典的ADO中的“provider=Mic ...

  9. Selenium之unittest测试框架详谈及实操

    申明:本文是基于python3.x及selenium3.x. unittest,也可以称为PyUnit,可以用来创建全面的测试套件,可以用于单元自动化测试(模块).功能自动化测试(UI)等等. 官方文 ...

  10. Java 反射实现实体转Map时,父类元素丢失

    public class BeanToMap { public static Map<String, Object> ConvertObjToMap(Object obj) { Map&l ...