閱讀549 返回首頁    go 阿裏雲 go 技術社區[雲棲]


麵試題:數組匹配

題目:

    有兩個數組A,B,所含元素相同,但順序不同,隻能取得A數組某值和B數組某值進行比較,比較結果為大於,小於或等於,但是不能取得同一數組A或B中的兩個數進行比較,也不能取得某數組中的某個值。寫一個算法實現正確匹配。

解題思路:遍曆兩個數組

代碼為:

// arrayMatch.cpp : 定義控製台應用程序的入口點。
//

#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] = {1,2,3,4,5,6,7,8,9,10};
	int b[10] = {10,6,4,5,1,8,7,9,3,2};

	int k = sizeof(a)/sizeof(int);
	matching(a,b,k);
	return 0;
}


最後更新:2017-04-03 16:49:00

  上一篇:go ffmpeg入門之 Tutorial02
  下一篇:go 經典麵試題:最長公共子序列