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 ...?)
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