Sum of two digits for S and continuous positive sequence for s

Source: Internet
Author: User

Question one: Enter an array with an ascending order and a number s, and find two numbers in the array so that they are exactly s. If there are many pairs of numbers and equals s, then the output can be any pair.

Analysis: Select both ends of the array, add them, and then compare with S, if larger than S, will point to the last bit of the array to move forward, if less than S, point to the first bit of the array to move backward one bit, and then continue the process. If equal to S, the two numbers are output.

Realize:

Bool findnumberswithsum (int data[],int length,int sum,int* num1,int* num2) {     bool found=false;    if (length<1| | num1==null| | Num2==null)         return found;         int ahead=length-1;    int behind=0;         while (Ahead>behind)     {         long long curSum=data[ahead]+data[behind];         if (cursum==sum)         {             *num1=data[behind];             *num2=data[ahead];             found=true;            break;        }         else if (cursum>sum)              ahead--;        else             behind++;    }     Return found;}

Title Two: Enter a positive number s, and print out all successive positive sequences (at least two numbers) for S. For example, input 15, because of 1+2+3+4+5=4+5+6=7+8=15, so the result prints out 3 sequential sequence 1~5,4~6,7~8.

Analysis: Set two numbers to represent the minimum and maximum values of the sequence small and Big,small are 1,big to 2, when small to big and less than S, big+1, if greater than S, small+1, and then continue. If equal, the output sequence. The condition of the cyclic stop is that the small is increased to (1+s)/2. Realize:

Void findcontinuoussequence (int sum) {    if (sum<3)          return;            int  samll=1;    int big=2;    int middle= (1+sum)/2;     int cursum=small+big;        while (small< Middle)     {        if (curSum==sum)              printcountinuoussequence (Small,big);                      while (Cursum>sum&&small<middle)         {             curSum-=small;            &nBsp;small++;            if (curSum==sum)                   Printcountinuoussequence (Small,big);        }         big++;        curSum+=big;             }}void printcontinuoussequence (Int small,int  big) {    for (int i=small;i<=big;++i)          printf ("%d  ", i);             printf ("\ n");}



This article from "Fairy Road thousands of dust Dream" blog, please be sure to keep this source http://secondscript.blog.51cto.com/9370042/1588074

Sum of two digits for S and continuous positive sequence for s

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.