2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

最初は( ゜д゜)o彡゜ん゛ーーー!

プログラムに大切なことって何だと思う?
そうだね、プロテインだね。

チェック処理とかは端折ってる

/** ここから定型文 */
ここで記載したクラスについては、
こっちの記事に書いてある、標準入力をList化したりしてるMainクラスから呼び出す予定だし・なんか継承したりしてるYO!!

ちな、Mainを実行して標準入力から入力するのクソ面倒くさいので、
Java編については問題の入力例をパラメータにしたテストクラスとかも作って公開するYO!!

開発・実行環境はこんな感じ
  • VSCode
  • Java 17
  • jUnit 5.9
  • maven

pom.xmlはこんな感じ

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>jp.co.asil</groupId>
    <artifactId>paiza202408</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
/** ここまで定型文 */

実装クラス

Janken.java
package jp.co.asil.paiza202408;

import java.util.List;
import java.util.Map;

public class Janken extends Question {
  private final int FINGER_PATTERN_COUNT = 3;
  private final String[] FINGER_PATTERNS = { "G", "C", "P" };
  private final int[] FINGER_COUNT = { 0, 2, 5 };
  private final Map<String, String> WIN_PATTERN = Map.of("G", "C", "C", "P", "P", "G");

  private int n;
  private int m;
  private String[] hands;

  public Janken(List<String> list) {
    super(list);
    int[] cond = List.of(list.get(0).split(" ")).stream().mapToInt(Integer::parseInt).toArray();
    this.n = cond[0];
    this.m = cond[1];
    this.hands = list.get(1).split("");
  }

  @Override
  public List<String> answer() {
    return List.of(String.valueOf(maxWinCount(0, 0, 0)));
  }

  private int maxWinCount(int round, int fingers, int wins) {
    if (round == this.n)
      return fingers == this.m ? wins : -1;
    int maxWins = -1;
    for (int i = 0; i < FINGER_PATTERN_COUNT; i++) {
      if (fingers + FINGER_COUNT[i] <= this.m)
        maxWins = Math.max(maxWins, maxWinCount(round + 1, fingers + FINGER_COUNT[i],
            wins + (WIN_PATTERN.get(FINGER_PATTERNS[i]).equals(this.hands[round]) ? 1 : 0)));
    }
    return maxWins;
  }
}

テストクラス

JankenTest.java
package jp.co.asil.paiza202408;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import java.util.List;

import org.junit.jupiter.api.Test;

public class JankenTest {
  @Test
  void testAnswer1() {
    Janken testClass = new Janken(List.of("4 7",
        "CGPC"));
    assertArrayEquals(testClass.answer().toArray(), new String[] { "4" });
  }

  @Test
  void testAnswer2() {
    Janken testClass = new Janken(List.of("5 10",
        "GPCPC"));
    assertArrayEquals(testClass.answer().toArray(), new String[] { "3" });
  }
}

解説はこっち

JavaScriptで書いたやつをJavaにリプレイスしただけの簡単なお仕事(手抜き

ハイ!ハハイ!エビバディパッション!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?