Find the two numbers in the ascending order array and the given value, in the ascending order Array
Detailed 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.