SWEA

[SWEA] 간단한 369게임

itsnot4me 2024. 9. 20. 21:33

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PTeo6AHUDFAUq&categoryId=AV5PTeo6AHUDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=1

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
	public static void main(String args[]) throws Exception
	{
		Scanner sc = new Scanner(System.in);
		int T;
		T=sc.nextInt();
		for(int a= 1; a <= T; a++)
		{
            String aText = String.valueOf(a);
            if((aText.indexOf("3")>=0)||(aText.indexOf("6")>=0)||(aText.indexOf("9")>=0)){
              	 int count = aText.length() - aText.replace("3", "").length();
				 int count2 = aText.length() - aText.replace("6", "").length();
                 int count3 = aText.length() - aText.replace("9", "").length();
                for(int i=0; i<count+count2+count3; i++){
                    System.out.print("-");
                }
                System.out.print(" ");
            }
            else
                System.out.print(aText + " ");
		}
	}
}

 

간단한 방법이 분명히 더 있겠지만

 

int를 String으로 변환(valueof), 3 or 6 or 9 가지고 있는지 확인(논리 연산자)

그렇다면 "-" 를 출력하되, 3 or 6 or 9 를 몇개 가지고 있는지 확인해서 출력하라 했으니 문자열 길이 length로 확인 후 그만큼 반복문 사용

 

그 외의 경우 그대로 출력

'SWEA' 카테고리의 다른 글

[SWEA] 파스칼의 삼각형  (1) 2024.09.24
[SWEA] 패턴 마디의 길이  (0) 2024.09.21
[SWEA] +=  (1) 2024.09.20
[SWEA] 거꾸로 출력해 보아요  (0) 2024.09.20
[SWEA] 더블더블  (0) 2024.09.20