我想要java中的acode用于交集和联合 [英] i want acode in java for intersection and union
本文介绍了我想要java中的acode用于交集和联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
帮我解答这个问题
解决方案
class IntersectionUnion
{
static int [] Intersection( int [] p1, int [] p2){
int count = 0 ;
for ( int i = 0 ; i < p1.length; i ++){
for ( int j = 0 ; j < p2.length ; j ++){
if (p1 [i] == p2 [j]){
count ++;
break ;
}
}
}
int [] result = new int [count];
count = 0 ;
for ( int i = 0 ; i < p1.length; i ++){
for ( int j = 0 ; j < p2.length ; j ++){
if (p1 [i] == p2 [j])
{
result [count ++] = p1 [一世];
break ;
}
}
}
返回结果;
}
静态 int [] Union( int [] p1, int [] p2){
int count = 0 ;
for ( int i = 0 ; i < p1.length; i ++){
for ( int j = 0 ; j < p2.length ; j ++){
if (p1 [i]!= p2 [j]){
count ++;
break ;
}
}
}
int [] result = new int [count];
count = 0 ;
for ( int i = 0 ; i < p1.length; i ++){
for ( int j = 0 ; j < p2.length ; j ++){
if (p1 [i]!= p2 [j])
{
result [count ++] = p1 [一世];
break ;
}
}
}
返回结果;
}
static int [] sortArray( int [] numarray)
{
int max = numarray.length;
for ( int i = 1 ; i < max; i ++){
for ( int j = 0 ; j < max - i; j ++ ){
if (numarray [j] > numarray [j + 1 ]){
int temp = numarray [j];
numarray [j] = numarray [j + 1 ];
numarray [j + 1 ] = temp;
}
}
}
返回 numarray;
}
静态 int [] Find_Unique_Elements( int [] numarray)
{
sortArray(numarray);
int element = numarray [ 0 ];
int count = 1 ;
for ( int i = 1 ; i < numarray.length; i ++)
{
if (element == numarray [i])
continue ;
else
{
element = numarray [i];
count ++;
}
}
int [] result = new INT 跨度> [数];
count = 0 ;
元素= numarray [ 0 ];
结果[count ++] = element;
for ( int i = 1 ; i < numarray.length; i ++){
if (element == numarray [i])
继续;
else
{
element = numarray [i];
结果[count ++] = element;
}
}
返回结果;
}
public static void main( String [] args)
{
int [ ] array1 = new int [] { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 , 95 , 85 , 75 , 65 , 55 , 45 , 35 , 25 ,< span class =code-digit> 15 , 05 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 , 50 , 55 };
int [] array2 = new int [] { 15 , 25 , 35 , 45 , 55 , 12 , 22 , 32 , 43 , 52 , 15 , 25 , 35 , 45 , 55 };
int [] p1 = Find_Unique_Elements(array1);
int [] p2 = Find_Unique_Elements(array2);
int [] InterRes = Intersection(array1,array2);
int [] unionRes = Union(arra1,array2);
系统。 out .print( \\第一个数组中的唯一排序元素:);
for ( int i = 0 ; i < p1.length; i ++)
System。 out .print( p1 [i] + );
系统。 out .print( \\第二个数组中的唯一排序元素:);
for ( int i = 0 ; i < p2.length; i ++)
System。 out .print( p2 [i] + );
系统。 out .print( \\ \\ n \\ n \\ n共同(交集)元素:);
for ( int i = 0 ; i < InterRes.length; i ++)
System。 out .print( InterRes [i] + );
for ( int i = 0 ; i < unionRes.length; i ++)
System。 out .print( unionRes [i] + );
}
}
试试吧,不会工作。
一切顺利
help me to get answer for this question
解决方案
class IntersectionUnion { static int[] Intersection(int[] p1, int[] p2){ int count = 0; for (int i = 0; i < p1.length; i++){ for (int j = 0; j < p2.length; j++){ if (p1[i] == p2[j]){ count++; break; } } } int[] result = new int[count]; count = 0; for (int i = 0; i < p1.length; i++){ for (int j = 0; j < p2.length; j++){ if (p1[i] == p2[j]) { result[count++] = p1[i]; break; } } } return result; } static int[] Union(int[] p1, int[] p2){ int count = 0; for (int i = 0; i < p1.length; i++){ for (int j = 0; j < p2.length; j++){ if (p1[i] != p2[j]){ count++; break; } } } int[] result = new int[count]; count = 0; for (int i = 0; i < p1.length; i++){ for (int j = 0; j < p2.length; j++){ if (p1[i] != p2[j]) { result[count++] = p1[i]; break; } } } return result; } static int[] sortArray(int[] numarray) { int max = numarray.length; for (int i = 1; i < max; i++){ for (int j = 0; j < max - i; j++){ if (numarray[j] > numarray[j + 1]){ int temp = numarray[j]; numarray[j] = numarray[j + 1]; numarray[j + 1] = temp; } } } return numarray; } static int[] Find_Unique_Elements(int[] numarray) { sortArray(numarray); int element = numarray[0]; int count = 1; for (int i = 1; i < numarray.length; i++) { if (element == numarray[i]) continue; else { element = numarray[i]; count++; } } int[] result = new int[count]; count = 0; element = numarray[0]; result[count++] = element; for (int i = 1; i < numarray.length; i++){ if (element == numarray[i]) continue; else { element = numarray[i]; result[count++] = element; } } return result; } public static void main(String[] args) { int[] array1 = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,95, 85, 75, 65, 55, 45, 35, 25, 15, 05,10, 15, 20, 25, 30, 35, 40, 45, 50, 55}; int[] array2 = new int[] { 15, 25, 35, 45, 55,12, 22, 32, 43, 52,15, 25, 35, 45, 55}; int [] p1 = Find_Unique_Elements(array1); int [] p2 = Find_Unique_Elements(array2); int [] InterRes = Intersection(array1, array2); int [] unionRes = Union(arra1,array2); System.out.print("\n\nUnique Sorted Elements in 1st Array: "); for(int i = 0; i < p1.length; i++) System.out.print(p1[i] + " "); System.out.print("\n\nUnique Sorted Elements in 2nd Array: "); for(int i = 0; i < p2.length; i++) System.out.print(p2[i] + " "); System.out.print("\n\nCommon (Intersection of) Elements : "); for(int i = 0; i < InterRes.length; i++) System.out.print(InterRes[i] + " "); for(int i = 0; i < unionRes.length; i++) System.out.print(unionRes[i] + " "); } }
Try it, wil work.
All the Best
这篇关于我想要java中的acode用于交集和联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文