【发布时间】:2013-02-06 13:34:52
【问题描述】:
考虑如果我有一个输入,其中作为输入的数组数量不固定,每个数组中的元素数量也不固定。所以每次我的输入不同时,
Example 1: 1st input
1st array= [2,3,4]
2nd array =[6,7,8,9]
3rd array=[5,3,12]
Example 2: 2nd input
1st array= [6,3,4,8]
2nd array =[6,7,4,9]
3rd array=[1,2,12]
4th array= [20,21,22,23,25]
需要的解决方案是,第一个数组被认为是一个参考数组,下一组数组要检查第一个数组(参考),要求第二个数组不应该有一个共同的元素第一个数组,然后继续检查第三个数组不应该与第一个数组有共同的元素。
from 1st Example
1st array= [2,3,4] -- reference array
1st array= [2,3,4] is compared to 2nd array =[6,7,8,9]
1st array= [2,3,4] is compared to 3rd array=[5,3,12]
solution needed:
+ print 1st array( reference array)
+ if no common elements found between 1st array and 2nd array, from ex. no common elements found, so print out the 2nd array.
+ same for 3rd array, from ex. there is common element(3 is present in both first and third array), so dont print.
我尝试过将输入存储在二维数组中,但我搞砸了。 请指导我使用您的算法/代码进行计算。
【问题讨论】:
-
您是如何获得输入的?从一个文件?来自
stdin?请展示获取输入的代码示例以及您对消除冗余算法的尝试 -
另外:当你找到一个或多个匹配的元素时,你不想打印整个数组,包括不匹配的元素?
-
对引用数组进行排序。二进制搜索查找。