Enter an array and a number that have been sorted in ascending order, and find two numbers in the array so that they are exactly the number entered. If there are many pairs of numbers and equals the input number, the output can be any pair.
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,// given a two array of the and
Output parameters (the memory area pointed to by the pointer is guaranteed to be valid):
int*pNum1// first number, corresponding to array index small
int *pNum2// the second number, which corresponds to the large array index
return value:
Find returns true, exception returns false
Full code:
#include "OJ.h"/* Function: Enter an array that has been sorted in ascending order and a number, and find two numbers in the array so that they are exactly the number entered. If there are many pairs of numbers and equals the input number, the output can be any pair. Input: int adata[] //ascending array unsigned int uilength//array element number int sum, //Given two array and output: int *pnum1 //First number, corresponding to the array index small int *pnum2 //second number, corresponding to the array index large return: found to return true, exception returned False*/bool findtwonumberswithsum (int adata[], unsigned int uilength, int sum, int *pnum1, int *pnum2) {/* Implement 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 written before the beginning of the preceding generation
if (uilength==0| | Uilength==1)
Actually submitted but, it's unbelievable,
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Find two numbers in ascending array and for a given value