Find the two numbers in the ascending order array and the given value, in the ascending order Array

Source: Internet
Author: User

Find the two numbers in the ascending order array and the given value, in the ascending order Array

  • Enter an array and a number that have been sorted in ascending order and search for two numbers in the array so that their sum is exactly the number entered. If there are multiple pairs of numbers and the sum is equal to the input number, output any one.

Detailed description:

  • Interface Description

Prototype:

Bool FindTwoNumbersWithSum (int aData [], unsignedint uiLength, int sum, int * pNum1, int * pNum2 );

Input parameters:

Int aData [] // ascending Array

Unsigned int uiLength // Number of array elements

Int sum, // specify the sum of the two Arrays

Output Parameters (the memory area pointed to by the pointer is valid ):

Int * pNum1 // the first number, corresponding to a small array index

Int * pNum2 // The second number, corresponding to the array index

Return Value:

Returns true if it finds and false if it returns an exception.


Complete code:

# Include "OJ. h "/* function: enter an array and a number that have been sorted in ascending order, and search for two numbers in the array so that their sum is exactly the number entered. If there are multiple pairs of numbers and the sum is equal to the input number, output any one. Input: int aData [] // ascending array unsigned int uiLength // Number of array elements int sum, // The sum of the given two arrays and the output: int * pNum1 // the first number, int * pNum2 // The second number corresponding to the small array index. If the index is large, true is returned. If an exception occurs, false is returned. */bool FindTwoNumbersWithSum (int aData [], unsigned int uiLength, int sum, int * pNum1, int * pNum2) {/* implement the function here */if (! AData | uiLength = 0 | uiLength = 1) return false; for (unsigned int I = 0; I <uiLength; I ++) for (unsigned int j = I + 1; j <uiLength; j ++) if (aData [I] + aData [j] = sum) {* pNum1 = aData [I]; * pNum2 = aData [j]; return true;} return false ;}

If
 if(uiLength==0||uiLength==1)

It's incredible that I cannot submit it,

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.