1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 5


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1475


image


๐Ÿ“Œ ํ’€์ด

์ฒ˜์Œ์—๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๊ตฌ์ƒ์„ ํ•˜๋ ค๊ณ  ํ–ˆ์—ˆ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์ด 6->9๋กœ ๋ฐ”๊พผ๋’ค Arrays.Sort๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ž์—๋ถ€ํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ์ƒˆ๋ฉด์„œ ๊ฐœ์ˆ˜๊ฐ€ ๋” ๋งŽ์•„์ง€๋ฉด ์ตœ๋Œ€ ์ˆซ์ž๋ฅผ ๋ฐ”๊พธ๋Š” ์‹์œผ๋กœ ๊ตฌ์ƒ์„ ํ•˜์˜€๋‹ค.
ํ•˜์ง€๋งŒ ์ด์ƒํ•˜๊ฒŒ ์ž‘๋™์„ ์•ˆํ–ˆ๋‹ค.


๐Ÿ“Œ Code

public static void House(int num) {
    int result = 0;
    String snum = Integer.toString(num);
    snum.replace("6","9");
    char[] chars = snum.toCharArray();

    char flag = chars[0];
    Arrays.sort(chars);

    int counts = 0;
    int finalNum =0;
    int finalCounts = 0;
    for (int i=0; i< chars.length; i++){
        if(flag==chars[i]){
            counts += 1;
            //System.out.println("counst: " + counts);
        }else {
            if(counts > finalCounts) {
                finalCounts = counts;
                finalNum = flag;
            }
            counts = 0;
            flag = chars[i];
        }
        if(i == chars.length-1){
            if(counts > finalCounts) {
                finalCounts = counts;
                finalNum = flag;
            }
        }
    }
    System.out.println(finalCounts);
    //System.out.println(finalNum);
}

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹จ์ˆœํ•˜๊ฒŒ 0-9๊นŒ์ง€์˜ ๊ณต๊ฐ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ ํ›„ ์•ˆ์—๋‹ค๊ฐ€ ๋„ฃ๊ธฐ๋กœ ํ•˜์˜€๋‹ค.
์ดํ›„ ๊ฐ ๋ฐฐ์—ด์˜ ์ˆซ์ž๊ฐ€ ๊ฐ€์žฅ ํฐ ๋ฐฐ์—ด์˜ ์ฃผ์†Œ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.
๋‹ค์Œ์€ ๊ทธ ์ฝ”๋“œ์ด๋‹ค.


package Implements;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

public class N1475 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter((System.out)));
        StringTokenizer st = new StringTokenizer(br.readLine());


        House(st.nextToken());
    }
    public static void House(String num){
        int[] memory = new int[10];
        for ( int i=0; i<10; i++) {
            memory[i] = 0;
        }

        String[] nums = num.split("");
        for ( String i : nums) {
            memory[Integer.parseInt(i)] += 1;
        }
        memory[6] = ((memory[6] + memory[9]) +1 )/2;

        int result = 0;
        for ( int i=0; i<9; i++) {
            if(result < memory[i]){
                result = memory[i];
            }
        }
        System.out.println(result);
    }

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜