Programming Algorithm-code of the same element of two Ascending Sequences (C)

Source: Internet
Author: User

Programming Algorithm-code of the same element of two Ascending Sequences (C)
Code of the same element of two Ascending Sequences (C)

 

 

TwoAscending sequenceTo useTwo pointers, Traverse in sequence. If the output is equal, if it is smaller than or greater than, a pointer is added.

Until all values are output.

 

Code:

 

/** Main. cpp ** Created on: 2014.9.19 * Author: spike */# include
 
  
Int Common (int data1 [], int length1, int data2 [], int leng2, int res []) {if (data1 = NULL | length1 <= 0 | data2 = NULL | length1 <= 0) return-1; int I = 0, j = 0, k = 0; while (I <length1 & j <length1) {if (data1 [I] = data2 [j]) {res [k ++] = data1 [I]; I ++; j ++;} else if (data1 [I] <data2 [j]) I ++; elsej ++;} return k;} int main (void) {int data1 [] = {1, 2, 4, 5, 6, 9, 11 }; int data2 [] = {2, 5, 6, 8, 12}; int Length1 = sizeof (data1)/sizeof (data1 [0]); int lengh2 = sizeof (data2)/sizeof (data2 [0]); int max = length1> leng22? Length1: leng2; int * res = new int [max]; int num = Common (data1, length1, data2, leng2, res); for (int I = 0; I
  
   Output:
   

 

 

2 5 6 


 

 

 

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.