Interview Questions: array matching

Source: Internet
Author: User

Question:

There are two arrays A and B, which contain the same elements, but the order is different. Only A value of array A and A value of array B can be obtained for comparison. The comparison result is greater than, less than or equal, however, you cannot compare two numbers in the same Array A or B, or obtain A value in an array. Write an algorithm to achieve correct matching.

Solution: traverse two Arrays

Code:

 

<SPAN style = "FONT-SIZE: 18px"> // arrayMatch. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <cstdlib> # include <iostream> using namespace std; void matching (int a [], int B [], int k) {int I = 0; while (I <k) {int j = 0; while (j <k) {if (a [I] = B [j]) {cout <"a [" <I <"]" <"match" <"B [" <j <"]" <endl; break;} j ++;} I ++;} cout <endl;} int _ tmain (int argc, _ TCHAR * argv []) {int a [10] = {,}; int B [10] = }; int k = sizeof (a)/sizeof (int); matching (a, B, k); return 0 ;}</SPAN>

 

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.