[Java] How to play rock-paper-scissors (equivalent to paiza rank A)

What is paiza?

paiza official website A place where you write a program and rank it and evaluate it You can appeal your programming ability to the companies you are looking for (I don't think it's practical, but if you solve it properly, you can see that it has the minimum ability.)

The problem to be solved this time

Problem: How to play rock-paper-scissors ・ Total number of games N ・ Total index M ・ Opponent's hand row S 3 are given as inputs, and the problem of determining the maximum number of wins against the opponent's hand when the total number of games and the total index are used exactly For details, link to

Answer code and result

import java.util.ArrayList;
import java.util.Scanner;

/**
 *How to put out the hands of rock-paper-scissors(equivalent to paiza rank A)
 */
public class AS001{

	public static void main(String[] args) {

		//input
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		char[] handArray = sc.next().toCharArray();


		//Opponent hand analysis
		int[] handCount = new int[3]; //{G, C, P}
		for(char hand : handArray) {
			switch(hand) {
			case 'G':
				handCount[0]++;
				break;
			case 'C':
				handCount[1]++;
				break;
			case 'P':
				handCount[2]++;
				break;
			}//switch
		}//for


		//Hand pattern enumeration
		ArrayList<Integer[]> patternList = new ArrayList<>(100);
		for(int i = 0; i <= n; i++) {
			for(int j = 0; j <= n; j++) {
				int yubi = i * 2 + j * 5; //index
				int remain = n - i - j; //Number of remaining games

				if(yubi == m && remain >= 0) {
					//A combination that uses up your fingers within the total number of games
					Integer[] pattern = {remain, i, j}; //{g, c, p}
					patternList.add(pattern);
				}else if(yubi > m || remain < 0) {
					//If the number of games is used up or the index is exceeded, the next loop
					break;
				}//if
			}//for
		}//for


		//Calculate the pattern of maximum wins
		int maxWin = 0;
		for(Integer[] pattern : patternList) {
			int win = 0;
			win += Math.min(handCount[0], pattern[2]); //The other party is goo, I am par
			win += Math.min(handCount[2], pattern[1]); //The other party is par, I am choki
			win += Math.min(handCount[1], pattern[0]); //The other party is Choki, I am Goo
			if(win > maxWin) maxWin = win;
		}//for


		//output
		System.out.println(maxWin);
	}//main
}//class

result

All execution time is within 0.10 seconds

How to put out rock-paper-scissors_result.png

Recommended Posts

[Java] How to play rock-paper-scissors (equivalent to paiza rank A)
How to make a Java container
[Java] How to create a folder
How to achieve paiza rank D
How to make a Java array
How to make a Java calendar Summary
[Introduction to Java] How to write a Java program
How to make a Discord bot (Java)
How to print a Java Word document
How to display a web page in Java
How to convert a solidity contract to a Java contract class
How to achieve paiza rank C (times method)
How to jump from Eclipse Java to a SQL file
java: How to write a generic type list [Note]
How to create a data URI (base64) in Java
[Java] How to get a request by HTTP communication
[Java] How to execute tasks on a regular basis
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Java] How to start a new line with StringBuilder
[Java] How to use Map
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java to play with Function
Java --How to make JTable
How to use java Optional
How to minimize Java images
How to write java comments
How to leave a comment
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to insert a video
How to create a method
[Introduction to rock-paper-scissors games] Java
How to create a lightweight container image for Java apps
How to deploy a simple Java Servlet app on Heroku
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
How to deploy a kotlin (java) app on AWS fargate
[Java] How to use substring to cut out a character string
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
[Spoilers] Long table eel shop (equivalent to paiza rank B)
How to play MIDI files using the Java Sound API
A story about misunderstanding how to use java scanner (memo)
How to study Java Silver SE 8
How to use Java HttpClient (Get)
Studying Java # 6 (How to write blocks)
[Java] How to update Java on Windows