Find two numbers in ascending array and for a given value

Source: Internet
Author: User

    • 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:

    • 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,// 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

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.