Enter an ordered array and a number to find two numbers in the array, so that their and exactly the number entered

Source: Internet
Author: User

Enter an ordered array and a number to 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. For example, input arrays 1, 2, 4, 7, 11, 15, and number 15. Because of the 4+11=15, the outputs are 4 and 11.

1#include <stdio.h>2#include <stdlib.h>3 4 voidFindtwo (int*array,intLenintsum)5 {6     intBeg =0;7     intEnd = len-1;8     intcursum;9      while(Beg <end)Ten     { OneCursum = Array[beg] +Array[end]; A         if(Cursum = =sum) -         { -printf"%d +%d =%d\n", array[beg],array[end],sum); thebeg++; -end--; -         } -         Else if(Cursum <sum) +beg++; -         Else +end--; A     } at } -  - intMain () - { -     intarray[6] = {0,2,4,7, One, the}; -Findtwo (Array,6, -); inSystem"Pause"); -     return 0; to}

Enter an ordered array and a number to find two numbers in the array, so that their and exactly the number entered

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.