๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SWEA

[SWEA] 1926. ๊ฐ„๋‹จํ•œ 369๊ฒŒ์ž„

by JulesJ 2022. 5. 29.
728x90

1926. ๊ฐ„๋‹จํ•œ 369๊ฒŒ์ž„ - D2

 

N์„ ์ž…๋ ฅ๋ฐ›๊ณ  1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋Š” ๋ฐ,

3,6,9๋ฅผ ํฌํ•จํ•œ ๊ฒฝ์šฐ ์ด ์ˆซ์ž๋“ค์„ "-"๋ฌธ์ž๋กœ ๋Œ€์ฒดํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

์šฐ์„ , N์„ ์ž…๋ ฅ๋ฐ›๊ณ  for๋ฌธ์„ ์ด์šฉํ•ด N๋งŒํผ 1๋ถ€ํ„ฐ ์ถœ๋ ฅํ•ด๋‚˜๊ฐ€๋Š”๋ฐ,

ํŠน์ •์กฐ๊ฑด์ผ ๊ฒฝ์šฐ "-"๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค.

 

1. N์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

2. 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ฆ๊ฐ€ํ•˜๋Š” i๋ฅผ ๋ฌธ์ž์—ด s๋กœ ์น˜ํ™˜ํ•˜๊ณ ,

3. 3,6,9๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ i๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

4. 3,6,9๋ฅผ ํฌํ•จํ•œ ๊ฒฝ์šฐ i์˜ ์ž๋ฆฟ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋ฌธ์ž์—ด s๋‚ด์—์„œ charAt์„ ์ด์šฉํ•ด, 3, 6, 9๋ฅผ ํฌํ•จํ•œ ๊ฒฝ์šฐ "-"๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

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 N = sc.nextInt();

		for(int i = 1; i <= N; i++)
		{
			String s = Integer.toString(i);
			if(s.contains("3")||s.contains("6")||s.contains("9")){
				int length = (int)(Math.log10(i)+1);
				for(int j = 0; j < length; j++){
					if(s.charAt(j) == '3' || s.charAt(j) == '6' || s.charAt(j) == '9'){
						System.out.print("-");
					}
				}
				System.out.print(" ");
			}else{
				System.out.print(i+" ");
			}
		}
	}
}

 

 

๋ ๐Ÿง

๋Œ“๊ธ€