搜索数组并比较它们

searching an array and compare them

本文关键字:比较 数组 搜索      更新时间:2023-10-16

写一个程序,从用户(max。各50人)。第一个数组的长度应该大于第二个;然后检查第二个数组中的所有成员是否存在在第一个,以相同的顺序,不管距离之间第一个数组中的成员。最后,程序告诉用户是否条件是否开启

数组的长度没有区别,只要您知道它们的长度。无论如何,你都要检查一下是否越过了他们。

这是一个算法:

if length of array2 is greater than 1, end.  // All of array can't be in array 1.
// Find the first element of array 2 that is in array 1:
// Note:  this only searches up to array1.length - array2.length,
//        because of the restriction of ordering and length of array 2.
key = first element of array 2.
found = false;
for (index = 0; index < (array1.length - array2.length); ++index)
  if (array[index] == key) then found = true, break out of loop. 
  end-for
if not found, end.  // No use in detailed searching.
// Now compare the elements of the two arrays, one by one, 
// starting with the second element of array2 and the element
// of array1 at (index + 1).

有更有效的算法,但这应该让你开始,直到有人发布确切的代码,所以你不必自己做任何工作。