2034-Everybody Loves AB (java)

Insert picture description here
The idea : to throw out the same elements in the B set and the A set, and the rest to a set, output.

import java.util.*;
public class Main {
    
    
public static void main(String[] args) {
    
    
	Scanner a=new Scanner(System.in);
	while(a.hasNext())
	{
    
    
		int n=a.nextInt();
		int m=a.nextInt();
		if(n==0&&m==0)
			break;
		int[] na=new int[n];
		int[] ma=new int[m];
		int[] ca=new int[n];				//创建最后的目标数组
		for(int i=0;i<n;i++)				//输入数据
			{
    
    
				na[i]=a.nextInt();
			}
		for(int j=0;j<m;j++)
			{
    
    
				ma[j]=a.nextInt();
			}
		int sort=0;int count=0;
		for(int i=0;i<n;i++)
		{
    
    
			boolean PanDuan=false;			//增加判断标志,判断ca[i]是否在另一数组中有相同元素
			for(int j=0;j<m;j++)
			{
    
    
				if(na[i]==ma[j])
					PanDuan=true;
			}
			if(PanDuan)						//如果ca[i]有相同元素则,令count加一,最后判断整个数组
				count++;
			else							//如果这一元素无相同,则将这一元素输入到目标数组中
			{
    
    
				ca[sort]=na[i];
				sort++;
			}
		}
		if(count==n)						//判断标志若等于N,代表着和另一数组元素完全相同,输入NULL即可
			System.out.print("NULL");
		else
		{
    
    
			for(int i=0;i<sort;i++)			//题目要求从小到大
			{
    
    
				for(int j=i+1;j<sort;j++)	//切记J从i+1开始
				{
    
    
					if(ca[i]>ca[j])
					{
    
    
						int t=ca[j];
						ca[j]=ca[i];
						ca[i]=t;
					}
				}
			}
			for(int i=0;i<sort;i++)
			{
    
    
				System.out.print(ca[i]+" ");
			}
		}
		System.out.println();				//注意格式
		}		
	}
}



If there is an error, please correct me

Guess you like

Origin blog.csdn.net/weixin_45956604/article/details/114290594