paizaラーニングレベルアップ問題集をJavaでやってみた

paiza×Qiita記事投稿キャンペーンということで、キャンペーン対象問題25問をJavaで解いてみました。
- - -
###### [文字の一致](https://paiza.jp/works/mondai/d_rank_skillcheck_sample/diff_str)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(sc.nextLine().equals(sc.nextLine()) ? "OK" : "NG");
		sc.close();
	}
}
```
- - -
###### [一番小さい値](https://paiza.jp/works/mondai/d_rank_skillcheck_sample/min_num)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int min = sc.nextInt();
        for (int i = 1; i < 5; i++) {
            min = Math.min(min, sc.nextInt());
        }
		sc.close();
        System.out.println(min);
	}
}
```
- - -
###### [足し算](https://paiza.jp/works/mondai/d_rank_skillcheck_sample/addition)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        System.out.println(sc.nextInt() + sc.nextInt());
		sc.close();
	}
}
```
- - -
###### [Eメールアドレス](https://paiza.jp/works/mondai/d_rank_skillcheck_archive/email_address)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        System.out.println(sc.nextLine() + "@" + sc.nextLine());
		sc.close();
	}
}
```
- - -
###### [N倍の文字列](https://paiza.jp/works/mondai/d_rank_skillcheck_archive/square)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        System.out.println("*".repeat(sc.nextInt()));
		sc.close();
	}
}
```
- - -
###### [宝くじ](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/lottery)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int b = sc.nextInt();
        int n = sc.nextInt();
        while (n-- > 0) {
            int a = sc.nextInt();
            if (a == b) {
                System.out.println("first");
            } else if (Math.abs(a - b) == 1) {
                System.out.println("adjacent");
            } else if ((a - b) % 10000 == 0) {
                System.out.println("second");
            } else if ((a - b) % 1000 == 0) {
                System.out.println("third");
            } else {
                System.out.println("blank");
            }
        }
		sc.close();
	}
}
```
- - -
###### [野球の審判](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/umpire)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int strike = 0;
        int ball = 0;
        while (N-- > 0) {
            String s = sc.next();
            if ("strike".equals(s)) {
                if (++strike == 3) {
                    System.out.println("out!");
                } else {
                    System.out.println("strike!");
                }
            } else if ("ball".equals(s)) {
                if (++ball == 4) {
                    System.out.println("fourball!");
                } else {
                    System.out.println("ball!");
                }
            }
        }
		sc.close();
	}
}
```
- - -
###### [みかんの仕分け](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/mikan)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        while (M-- > 0) {
            int w = sc.nextInt();
            System.out.println(Math.max((w + N / 2) / N, 1) * N);
        }
		sc.close();
	}
}
```
- - -
###### [Fizz Buzz](https://paiza.jp/works/mondai/c_rank_skillcheck_sample/fizz-buzz)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        for (int i = 1; i <= N; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                System.out.println("Fizz Buzz");
            } else if (i % 3 == 0) {
                System.out.println("Fizz");
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
		sc.close();
	}
}
```
- - -
###### [残り物の量](https://paiza.jp/works/mondai/c_rank_skillcheck_archive/leftover)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        double m = sc.nextDouble();
        double p = sc.nextDouble();
        double q = sc.nextDouble();
        System.out.println(m * (100 - p) * (100 - q) / 10000);
		sc.close();
	}
}
```
- - -
###### [3Dプリンタ](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/3d_printer)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int X = sc.nextInt();
        int Y = sc.nextInt();
        int Z = sc.nextInt();
        sc.nextLine();
        char[][][] s = new char[Z][][];
        for (int z = 0; z < Z; z++) {
            s[z] = new char[X][];
            for (int x = 0; x < X; x++) {
                s[z][x] = sc.nextLine().toCharArray();
            }
            String bar = sc.nextLine();
        }
		sc.close();
        while (Z-- > 0) {
            for (int y = 0; y < Y; y++) {
                char c = '.';
                for (int x = 0; x < X; x++) {
                    if (s[Z][x][y] == '#') {
                        c = '#';
                        break;
                    }
                }
                System.out.print(c);
            }
            System.out.println();
        }
	}
}
```
- - -
###### [神経衰弱](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/concentration)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int H = sc.nextInt();
        int W = sc.nextInt();
        int N = sc.nextInt();
        sc.nextLine();
        String t[][] = new String[H][];
        for (int i = 0; i < H; i++) {
            t[i] = sc.nextLine().split(" ");
        }
        int C[] = new int[N];
        int P = 0;
        int L = sc.nextInt();
        while (L-- > 0) {
            int a = ~-sc.nextInt();
            int b = ~-sc.nextInt();
            int A = ~-sc.nextInt();
            int B = ~-sc.nextInt();
            if (t[a][b].equals(t[A][B])) {
                C[P] += 2;
            } else {
                P = -~P % N;
            }
        }
        for (int i = 0; i < N; i++) {
            System.out.println(C[i]);
        }
		sc.close();
	}
}
```
- - -
###### [みんなでしりとり](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/word_chain)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        int M = sc.nextInt();
        HashSet<String> D = new HashSet<>();
        while (K-- > 0) {
            D.add(sc.next());
        }
        TreeSet<Integer> alive = new TreeSet<>();
        for (int i = 1; i <= N; i++) {
            alive.add(i);
        }
        String bef = "";
        int P = 1;
        while (M-- > 0) {
            String s = sc.next();
            if (D.contains(s) && ("".equals(bef) || bef.charAt(bef.length() - 1) == s.charAt(0)) && s.charAt(s.length() - 1) != 'z') {
                D.remove(s);
                bef = s;
            } else {
                alive.remove(P);
                bef = "";
            }

            do {
                P = -~(P % N);
            } while (!alive.contains(P));
        }
		sc.close();
        System.out.println(alive.size());
        for (int a : alive) {
            System.out.println(a);
        }
	}
}
```
- - -
###### [長テーブルのうなぎ屋](https://paiza.jp/works/mondai/b_rank_skillcheck_sample/long-table)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        boolean[] seats = new boolean[N];
        int C = 0;
        while (M-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            boolean can_sit = true;
            for (int i = 0; i < a; i++) {
                if (seats[(b + i) % N]) {
                    can_sit = false;
                    break;
                }
            }
            if (can_sit) {
                for (int i = 0; i < a; i++) {
                    seats[(b + i) % N] = true;
                }
                C += a;
            }
        }
		sc.close();
        System.out.println(C);
	}
}
```
- - -
###### [名刺バインダー管理](https://paiza.jp/works/mondai/b_rank_skillcheck_archive/name_card)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
		sc.close();
		System.out.println(2 * n * ((m - 1) / (2 * n) * 2 + 1) + 1 - m);
	}
}
```
- - -
###### [本の整理](https://paiza.jp/works/mondai/real_event/book_sort)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] A = new int[-~N];
        int[] B = new int[-~N];
        for (int i = 1; i <= N; i++) {
            A[i] = sc.nextInt();
            B[A[i]] = i;
        }
		sc.close();
        int C = 0;
        for (int i = 1; i <= N; i++) {
            if (A[i] != i) {
                B[A[i]] = B[i];
                A[B[i]] = A[i];
                C++;
            }
        }
        System.out.println(C);
	}
}
```
- - -
###### [山折り谷折り](https://paiza.jp/works/mondai/real_event/origami)
```java
import java.util.*;

public class Main {
    private static String reverse(String s) {
        String t = new String();
        int n = s.length();
        while (n-- > 0) {
            t += (char) ('0' + '1' - s.charAt(n));
        }
        return t;
    }

    private static String origami(int n) {
        if (n == 0) {
            return new String();
        }
        String s = origami(~-n);
        return s + "0" + reverse(s);
    }
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        System.out.println(origami(sc.nextInt()));
		sc.close();
	}
}
```
- - -
###### [ハノイの塔](https://paiza.jp/works/mondai/real_event/hanoi)
```java
import java.util.*;

public class Main {
    static int N;
    static int t;

    private static void hanoi(int n, Stack<Integer> src, Stack<Integer> dst, Stack<Integer> tmp) {
        if (t == 0) {
            return;
        }
        if (n == 1) {
            dst.push(src.pop());
            if (--t == 0) {
                return;
            }
        } else {
            hanoi(~-n, src, tmp, dst);
            hanoi(1, src, dst, tmp);
            hanoi(~-n, tmp, dst, src);
        }
    }
    
    @SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        t = sc.nextInt();
		sc.close();
        Stack<Integer>[] piles = (Stack<Integer>[]) (new Stack<?>[3]);
        for (int i = 0; i < piles.length; i++) {
            piles[i] = new Stack<Integer>();
        }
        for (int i = N; i > 0; i--) {
            piles[0].push(i);
        }
        hanoi(N, piles[0], piles[2], piles[1]);
        for (Stack<Integer> pile : piles) {
            if (pile.empty()) {
                System.out.println("-");
            } else {
                for (int i = 0; i < pile.size(); i++) {
                    if (i > 0) {
                        System.out.print(' ');
                    }
                    System.out.print(pile.get(i));
                }
                System.out.println();
            }
        }
	}
}
```
- - -
###### [じゃんけんの手の出し方](https://paiza.jp/works/mondai/a_rank_skillcheck_sample/janken)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        String s = sc.nextLine();
		sc.close();
        int G = 0;
        int C = 0;
        int P = 0;
        for (char c : s.toCharArray()) {
            if (c == 'G') G++;
            else if (c == 'C') C++;
            else if (c == 'P') P++;
        }
        int ans = 0;
        for (int p = M % 2; p <= M / 5; p += 2) {
            int c = (M - 5 * p) / 2;
            if (c > N) continue;
            int g = N - c - p;
            if (g < 0) continue;
            ans = Math.max(ans, Math.min(g, C) + Math.min(c, P) + Math.min(p, G));
        }
        System.out.println(ans);
	}
}
```
- - -
###### [お菓子の詰め合わせ](https://paiza.jp/works/mondai/a_rank_skillcheck_archive/school_hiking)
```java
import java.util.*;

public class Main {
    static int N;
    static int X;
    static int[] P;
    static int M;

    private static int dfs(Stack<Integer> stack, int amount) {
        if (stack.size() == M) {
            if (amount > X) {
                return -1;
            } else {
                return amount;
            }
        } else {
            int max = 0;
            for (int i = (stack.empty() ? 0 : -~stack.peek()); i < N; i++) {
                stack.push(i);
                max = Math.max(max, dfs(stack, amount + P[i]));
                stack.pop();
            }
            return max;
        }
    }
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        X = sc.nextInt();
        P = new int[N];
        for (int i = 0; i < N; i++) {
            P[i] = sc.nextInt();
        }
		sc.close();
        Arrays.sort(P);
        int S = 0;
        M = 0;
        while (M < N) {
            if (S + P[M] > X) {
                break;
            }
            S += P[M++];
        }
        System.out.println(X - dfs(new Stack<Integer>(), 0));
	}
}
```
- - -
###### [十億連勝](https://paiza.jp/works/mondai/real_event/continuous_winning)
```java
import java.util.*;

public class Main {
    static final long MOD = 1000000000;
    
    @SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int X = sc.nextInt();
        HashMap<Integer, Long>[] states = (HashMap<Integer, Long>[])(new HashMap<?, ?>[2]);
        for (int i = 0; i < 2; i++) {
            states[i] = new HashMap<>();
        }
        states[0].put(0, 1L);
        while (N-- > 0) {
            int a = sc.nextInt();
            HashMap<Integer, Long>[] temp = (HashMap<Integer, Long>[])(new HashMap<?, ?>[2]);
            for (int i = 0; i < 2; i++) {
                temp[i] = new HashMap<>();
            }
            for (int i = 0; i < 2; i++) {
                for (int w : states[i].keySet()) {
                    if (w + a <= X) {
                        temp[i].put(w + a, (temp[i].getOrDefault(w + a, 0L) + states[i].get(w)) % MOD);
                        temp[i].put(0, (temp[i].getOrDefault(0, 0L) + a * states[i].get(w)) % MOD);
                    } else {
                        temp[1].put(0, (temp[1].getOrDefault(0, 0L) + states[i].get(w)) % MOD);
                        temp[i].put(0, (temp[i].getOrDefault(0, 0L) + (X - w) * states[i].get(w)) % MOD);
                    }
                }
            }
            states = temp;
        }
		sc.close();

        long ans = states[0].getOrDefault(X, 0L);
        for (long v : states[1].values()) {
            ans = (ans + v) % MOD;
        }
        System.out.println(ans);
	}
}
```
- - -
###### [文字列収集](https://paiza.jp/works/mondai/real_event/word_collection)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        Map<String, Integer> dict = new HashMap<>();
        while (N-- > 0) {
            String s = sc.next();
            int p = sc.nextInt();
            int l = s.length();
            for (int i = 1; i <= l; i++) {
                String t = s.substring(0, i);
                dict.put(t, dict.getOrDefault(t, 0) + p);
            }
        }
        while (M-- > 0) {
            System.out.println(dict.getOrDefault(sc.next(), 0));
        }
		sc.close();
	}
}
```
- - -
###### [mod7占い](https://paiza.jp/works/mondai/s_rank_skillcheck_sample/mod7)
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long[] C = new long[7];
        while (N-- > 0) {
            long a = sc.nextLong();
            C[(int)(a % 7L)]++;
        }
		sc.close();
        long ans = 0;
        for (int i = 0; i < 7; i++) {
            for (int j = i; j < 7; j++) {
                int k = (14 - i - j) % 7;
                if (k < j) continue;
                if (i == j && j == k) {
                    ans += C[i] * (C[i] - 1) * (C[i] - 2) / 6;
                } else if (i == j) {
                    ans += C[i] * (C[i] - 1) * C[k] / 2;
                } else if (j == k) {
                    ans += C[i] * C[j] * (C[j] - 1) / 2;
                } else {
                    ans += C[i] * C[j] * C[k];
                }
            }
        }
        System.out.println(ans);
	}
}
```
- - -
###### [島探し](https://paiza.jp/works/mondai/s_rank_skillcheck_sample/search-island)
```java
import java.util.*;

public class Main {
    static final int[][] D = new int[][]{ { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };

    static void dfs(String[][] A, int y, int x) {
        A[y][x] = "0";
        for (int[] d : D) {
            int i = y + d[0];
            int j = x + d[1];
            if (0 <= i && i < A.length && 0 <= j && j < A[i].length && "1".equals(A[i][j])) {
                dfs(A, i, j);
            }
        }
    }
    
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int M = sc.nextInt();
        int N = sc.nextInt();
        sc.nextLine();
        String[][] A = new String[N][];
        for (int i = 0; i < N; i++) {
            A[i] = sc.nextLine().split(" ");
        }
		sc.close();

        int ans = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if ("1".equals(A[i][j])) {
                    dfs(A, i, j);
                    ans++;
                }
            }
        }
        System.out.println(ans);
	}
}
```
- - -
###### [村人の友好関係](https://paiza.jp/works/mondai/s_rank_skillcheck_archive/group_popularity)
```java
import java.util.*;

class Edge {
	private int[] e;

	public int getA() {
		return e[0];
	}

	public int getB() {
		return e[1];
	}

	private int f;

	public int getF() {
		return f;
	}

	public Edge(int a, int b, int f) {
		this.e = new int[] { a, b };
		this.f = f;
	}
}

class UnionFind {
	private int[] A;

	private int root(int v) {
		if (A[v] < 0)
			return v;
		A[v] = root(A[v]);
		return A[v];
	}

	public boolean unite(int v, int u) {
		int rv = root(v);
		int ru = root(u);
		if (rv == ru)
			return false;
		if (A[rv] > A[ru]) {
			A[rv] = ru;
		} else {
			if (A[rv] == A[ru])
				A[rv]--;
			A[ru] = rv;
		}
		return true;
	}

	public UnionFind(int n) {
		A = new int[-~n];
		Arrays.fill(A, -1);
	}
}

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int Q = sc.nextInt();
		PriorityQueue<Edge> PQ = new PriorityQueue<>(Comparator.comparingInt(Edge::getF).reversed());
		while (M-- > 0) {
			PQ.add(new Edge(sc.nextInt(), sc.nextInt(), sc.nextInt()));
		}
		UnionFind uf = new UnionFind(N);
		List<Edge> MST = new ArrayList<>();
		while (!PQ.isEmpty()) {
			Edge e = PQ.poll();
			if (uf.unite(e.getA(), e.getB()))
				MST.add(e);
		}
		Set<Integer> set = new HashSet<>();
		while (Q-- > 0) {
			String op = sc.next();
			int q = sc.nextInt();
			if ("+".equals(op))
				set.add(q);
			else if ("-".equals(op))
				set.remove(q);
			int p = 0;
			for (Edge e : MST) {
				if (set.contains(e.getA()) != set.contains(e.getB())) {
					p = e.getF();
					break;
				}
			}
			System.out.println(p);
		}
		sc.close();
	}
}
```