Find zero pairs

Source: Internet
Author: User

Question: Design and implement an algorithm (C ++ function) that, given an array of integers, determines whether the sum of any two distinct elements is zero. assume, of course, that there might be positive and negative values in the array (I. e. don't just look for a pair of zeroes !). Test IDE: Microsoft Visual Studio 2005 test OS: Microsoft Windows 7 The code as following may solve this problem: [cpp] # include "stdafx. h "# include <iostream >#include <algorithm> void FindZeroPairs (int data [], int nLen) {int pLeft = 0; int pRight = nLen-1; while (pLeft <pRight) {if (0 = data [pLeft] + data [pRight]) {www.2cto.com printf ("Zero pair: % d and % d \ n ", data [pLeft], data [pRight]); -- pRight; ++ pLeft; continue;} if (0 <data [pLeft] + data [pRight]) {-- pRight; continue;} if (0> data [pLeft] + data [pRight]) {++ pLeft;} int _ tmain (int argc, _ TCHAR * argv []) {int nLen; printf ("Input length of integer array:"); scanf ("% d", & nLen ); // allocate space for array with length of nLen int * pData = new int [nLen]; int I = 0; while (I <nLen) {scanf ("% d ", & pData [I]); ++ I;} // sort array std: sort (pData, pData + nLen); for (int I = 0; I <nLen; ++ I) {printf ("% d", pData [I]);} printf ("\ n"); FindZeroPairs (pData, nLen); return 0 ;} # include "stdafx. h "# include <iostream >#include <algorithm> void FindZeroPairs (int data [], int nLen) {int pLeft = 0; int pRight = nLen-1; while (pLeft <pRight) {if (0 = data [pLeft] + data [pRight]) {printf ("Zero pair: % d and % d \ n ", data [pLeft], data [pRight]); -- pRight; ++ pLeft; continue;} if (0 <data [pLeft] + data [pRight]) {-- pRight; continue;} if (0> data [pLeft] + data [pRight]) {++ pLeft;} int _ tmain (int argc, _ TCHAR * argv []) {int nLen; printf ("Input length of integer array:"); scanf ("% d", & nLen ); // allocate space for array with length of nLenint * pData = new int [nLen]; int I = 0; while (I <nLen) {scanf ("% d ", & pData [I]); ++ I;} // sort arraystd: sort (pData, pData + nLen); for (int I = 0; I <nLen; ++ I) {printf ("% d", pData [I]);} printf ("\ n"); FindZeroPairs (pData, nLen); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.