본문 바로가기

코딩테스트 준비/SW Expert Academy

SW Expert Academy [D2-1926] Java 풀이 : 간단한 369게임

간단한 369게임

 

처음 풀이

 

package difficulty.level02;

import java.util.Scanner;

public class no1926 {
	
	/**
	 * 1926. 간단한 369게임
	*/
	
	public static void main(String args[]) throws Exception
	{
		Scanner sc = new Scanner(System.in);
		int T;
		T=sc.nextInt();
		
		for(int test_case = 1; test_case <= T; test_case++)
		{
			String num = Integer.toString(test_case);
			char[] numChar = num.toCharArray();
			int cnt = 0;
			
			for(int i=0; i<numChar.length; i++) {
				if(numChar[i] == '3' || numChar[i] == '6' || numChar[i] == '9') {
					cnt++;
				}
			}
			
			if(cnt==0) {
				System.out.print(num+" ");
			}else {
				for(int i=0; i<cnt; i++) {
					System.out.print("-");
				}
				System.out.print(" ");
			}
		}
	}
}

 

좀 더 간단한 풀이

 

package difficulty.level02;

import java.util.Scanner;

public class no1926 {
	
	/**
	 * 1926. 간단한 369게임
	*/
	
	public static void main(String args[]) throws Exception
	{
		Scanner sc = new Scanner(System.in);
		int T;
		T=sc.nextInt();
		
		for(int test_case = 1; test_case <= T; test_case++)
		{
			String num = Integer.toString(test_case);
			char[] numChar = num.toCharArray();
			
			String result = "";
			
			for(int i=0; i<numChar.length; i++) {
				if(numChar[i] == '3' || numChar[i] == '6' || numChar[i] == '9') {
					result += "-";
				}
			}
			
			if(result == "") {
				System.out.print(num+" ");
			}else {
				System.out.print(result+" ");
			}
		}
	}
}

 

Github 주소

https://github.com/MIN-04/CodingTest/blob/b491bf4423f325035afbd0cc9941cfa54b227fbd/SW_Expert_Academy/DLevel02/no1926.java

 

MIN-04/CodingTest

코딩테스트 준비 / 문제 풀이. Contribute to MIN-04/CodingTest development by creating an account on GitHub.

github.com