给定一个整数数组,判断能否从中找出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. shell入门笔记2:字符串、数组、echo与printf

    说明: 本文是关于http://c.biancheng.net/cpp/shell/的相关笔记 shell字符串 字符串可以用单引号,也可以用双引号,也可以不用引号. 1 #!/bin/bash 2 ...

  2. ABP领域层知识回顾之---工作单元

    1. 前言   在上一篇博文中(http://www.cnblogs.com/xiyin/p/6842958.html) 我们讲到了ABP领域层的仓储.这边博文我们来讲 工作单元.个人觉得比较重要.文 ...

  3. no zuo no die

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; name ...

  4. Bubble Cup X - Finals [Online Mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...

  5. 习题9-4 uva 1630

    题意: 给你一串数字,要求你对其进行折叠使其长度最短. 折叠情况:全是一个字母 & 重复的字符串 AAAAAAAAAABABABCCD    -->   9(A)3(AB)CCD NEE ...

  6. Linux中LCD设备驱动-framebuffer(帧缓冲)【】

    转自:https://blog.csdn.net/linux_devices_driver/article/details/7079442 1.framebuffer 帧缓冲     帧缓冲(fram ...

  7. SQL Server 连接 MySQL

    1.在SQL SERVER服务器上安装MYSQL ODBC驱动; 驱动下载地址:http://dev.mysql.com/downloads/connector/odbc/ 2.安装好后,在管理工具- ...

  8. Cisco 交换机配置的基本命令

    1.不同的vlan 不同vlan需要路由 在路由的端口设置多个IP段 交换机模拟器实验六 2.查看端口名字 SWA#sh vlan    default  Active  F0/1, F0/2, F0 ...

  9. jquery 引号问题

    varFrozenColumns="[[{'field':'CZ','title':'操作','width':80,'align':'center','formatter':function ...

  10. String String Buffer String Builder

    如题,在java中这是一个典型的问题. 在stackoverflow上已经有很多相似的问题被提问,并且有很多不正确或不完整的答案.如果你不往深处想,这是一个很简单的问题.但如果深入思考,它却很让人迷惑 ...