An unordered natural number sequence has n natural numbers, and the sum of the two is quickly found to be two integers of M and output. -TV

Source: Internet
Author: User



Idea: First of all unordered natural number sequence (ascending), and then define I and J points to the two ends of the sequence, referring to the sum of two and M compared, larger than M, then J left shift, smaller than m, then I move right.



The code is as follows:



#include <stdio.h>
#include <stdlib.h>


Quick Line
void sort (int *a, int left, int. right)
{
if (left >= right)
{
return;
}
int i = left;
int j = right;
int key = A[left];

while (I < j)/* control in the group look for once * *
{
Two-way comparison
while (I < J && Key <= A[j])
{
j--; Looking forward
}

A[i] = A[j]; Swap location

while (I < J && Key >= A[i])
{
i++; Looking backwards
}

A[J] = A[i]; Swap location
}

A[i] = key;
Sort (A, left, i-1);
Sort (A, i + 1, right);
}

int main ()
{
int a[10] = { -2,1,3,4,-5,12,45,34,23,9};
int M = 26;
int I, J;
for (i = 0; i <; i++)
printf ("%d", a[i]);
printf ("\ n");
Sort (A, 0, 9);
for (i = 0; i <; i++)
printf ("%d", a[i]);
printf ("\ n");
i = 0;
j = 9;
while (I < j)
{
if (A[i] + a[j] = = M)
Break
if (A[i] + a[j] > M)
j--;
Else
i++;
}
if (i! = j)
printf ("%d +%d =%d\n", a[i],a[j],m);
Else
printf ("NO find!\n");
return 0;
}


An unordered natural number sequence has n natural numbers, and the sum of the two is quickly found to be two integers of M and output. -TV


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.