I wrote EX25 of AtCoder Programming Guide for beginners (APG4b) in java.

Introduction

This article is about the content in AtCoder

AtCoder Programming Guide for beginners (APG4b)

I tried to write the task EX25 --Set operation posted in Java, so I will publish the code as a memorandum. I would appreciate it if you could read it.

If you have any points that are difficult to read, mistakes, or impressions, please let us know in the comments.

Let's use the bit operator! I'm dealing directly with integer values without the guts ... (I wish I had implemented the bitSet in C ++. Is there ...?)

code

Main.java


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		Integer[] A = new Integer[N];
		for(int i = 0;i < N;i++) {
			A[i] = sc.nextInt();
		}
		int M = sc.nextInt();
		Integer[] B = new Integer[M];
		for(int i = 0;i < M;i++) {
			B[i] = sc.nextInt();
		}
		String command = sc.next();
		int x = 0;
		if(command.equals("subtract")) {
			x = sc.nextInt();
		}
		sc.close();
		switch(command) {
		case "intersection":
			printResult(intersection(A, B));
			break;
		case "union_set":
			printResult(unionset(A, B));
			break;
		case "symmetric_diff":
			printResult(symmetricdiff(A, B));
			break;
		case "subtract":
			printResult(subtract(A, x));
			break;
		case "increment":
			printResult(increment(A));
			break;
		case "decrement":
			printResult(decrement(A));
			break;
		default:
			break;
		}
	}
	/**
	 *Returns a set of elements commonly contained in A and B
	 * @param A
	 * @param B
	 * @return Array of common elements
	 */
	private static Integer[] intersection(Integer[] A,Integer[] B) {
		List<Integer> Alist = Arrays.asList(A);
		List<Integer> Blist = Arrays.asList(B);
		List<Integer> result = new ArrayList<>();
		for(Integer i : Alist) {
			if(Blist.contains(i)) {
				result.add(i);
			}
		}
		if(result.size()==0) return null;
		Collections.sort(result);
		return  result.toArray(new Integer[result.size()]);
	}

	/**
	 *Returns a set of elements contained in at least one of A and B
	 * @param A
	 * @param B
	 * @return An array of elements contained in at least one
	 */
	private static Integer[] unionset(Integer[] A,Integer[] B) {
		Set<Integer> result = new TreeSet<>(Comparator.naturalOrder());
		for(Integer i : A) result.add(i);
		for(Integer i : B) result.add(i);
		return result.size()==0 ? null : result.toArray(new Integer[result.size()]);
	}

	/**
	 *Returns a set of elements contained in only one of A and B
	 * @param A
	 * @param B
	 * @return An array of elements contained in only one
	 */
	private static Integer[] symmetricdiff(Integer[] A,Integer[] B) {
		List<Integer> intersection = Arrays.asList(intersection(A, B));
		List<Integer> union = Arrays.asList(unionset(A, B));
		List<Integer> result = new ArrayList<>();
		for(Integer i : union) {
			if(union.contains(i) && !intersection.contains(i)) {
				result.add(i);
			}
		}
		if(result.size()==0) return null;
		Collections.sort(result);
		return result.toArray(new Integer[result.size()]);
	}

	/**
	 *Exclude the value x from the set A
	 * @param A
	 * @param x
	 * @return
	 */
	private static Integer[] subtract(Integer[] A, int x) {
		List<Integer> list = Arrays.asList(A);
		List<Integer> result = new ArrayList<>();
		for(Integer i : list) {
			if(i != x) {
				result.add(i);
			}
		}
		if(result.size()==0) return null;
		Collections.sort(result);
		return result.toArray(new Integer[result.size()]);
	}

	/**
	 *Add 1 to all elements of A.
	 */
	private static Integer[] increment(Integer[] A) {
		List<Integer> result = new ArrayList<>();
		for(Integer i : A) {
			i++;
			if(i.equals(50)) {
				i = 0;
			}
			result.add(i);
		}
		if(result.size()==0) return null;
		Collections.sort(result);
		return result.toArray(new Integer[result.size()]);
	}

	/**
	 *Subtract 1 from all elements of A.
	 */
	private static Integer[] decrement(Integer[] A) {
		List<Integer> result = new ArrayList<>();
		for(Integer i : A) {
			i--;
			if(i.equals(-1)) {
				i = 49;
			}
			result.add(i);
		}
		if(result.size()==0) return null;
		Collections.sort(result);
		return result.toArray(new Integer[result.size()]);
	}

	/**
	 *Result output
	 */
	private static void printResult(Integer[] result) {
		if(result==null) {
			System.out.println("");
			return;
		}
		if(result.length > 1) {
			for(int i = 0;i < result.length-1;i++) {
				System.out.print(result[i] + " ");
			}
		}
		System.out.println(result[result.length-1]);
	}
}

Recommended Posts

I wrote EX25 of AtCoder Programming Guide for beginners (APG4b) in java.
[For beginners] Explanation of classes, instances, and statics in Java
[For beginners] Summary of java constructor
Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
I wrote Goldbach's theorem in java
[Java] I participated in ABC-188 of Atcorder.
Java Programming Style Guide for the Java 11 Era
Introduction of New Generation Java Programming Guide (Java 10)
Introduction of New Generation Java Programming Guide (Java 11)
Introduction of New Generation Java Programming Guide (Java 12)
[For beginners] I tried using DBUnit in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
I wrote a primality test program in Java
I wrote a prime factorization program in Java
[Introduction to Java] Basics of java arithmetic (for beginners)
[For beginners] DI ~ The basics of DI and DI in Spring ~
[For beginners] Quickly understand the basics of Java 8 Lambda
I wrote about Java downcast in an easy-to-understand manner
[For beginners] Minimum sample to display RecyclerView in Java
Introduction to Java for beginners Basic knowledge of Java language ①
I wrote an overview of Chef in an easy-to-understand manner
I tried using an extended for statement in Java
List of frequently used Java instructions (for beginners and beginners)
~ I tried to learn functional programming in Java now ~
List of MySQL sentences for programming beginners * Personal memo
Constraint programming in Java
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
I tried to make a client of RESAS-API in Java
AtCoder dwango Programming Contest B in Ruby, Perl and Java
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
[Java] Beginner's understanding of Servlet-②
Java debug execution [for Java beginners]
[Java] Beginner's understanding of Servlet-①
[Java] Summary of for statements
Java for beginners, data hiding
Implementation of gzip in java
Implementation of tri-tree in Java
Java application for beginners: stream
I tried metaprogramming in Java
[Java] Basic terms in programming
I will explain the nesting of for statements that kill beginners
Kantai Collection Java # 0 What is Object Oriented Programming (OOP)? [For beginners]
Handle business logic for a set of Entity in Java class
I tried setting Java beginners to use shortcut keys in eclipse
I wrote a Stalin sort that feels like a mess in Java
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java