[๋ฐฑ์ค] ๐ฅ 1475 ๋ฐฉ ๋ฒํธ
๐ ๋์ด๋
๐ฅ Silver 5
๐ ๋ฌธ์
https://www.acmicpc.net/problem/1475
๐ ํ์ด
์ฒ์์๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๊ตฌ์์ ํ๋ ค๊ณ ํ์๋ค.
๋ค์๊ณผ ๊ฐ์ด 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
์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
๊ฐ์ธ ๊ณต๋ถ ๊ธฐ๋ก์ฉ ๋ธ๋ก๊ทธ์
๋๋ค.
ํ๋ฆฌ๊ฑฐ๋ ์ค๋ฅ๊ฐ ์์ ๊ฒฝ์ฐ ์ ๋ณดํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.๐