๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ๋ฌธ์ œํ’€๊ณ  ์ •๋ฆฌํ•˜๊ธฐ +/๋ฐฑ์ค€

[๋ฐฑ์ค€ Java] 1157: ๋‹จ์–ด ๊ณต๋ถ€

by ์ข…์ด๋นจ๋Œ€ 2023. 9. 24.
TOP

๋ชฉ์ฐจ

    1. ๋ฌธ์ œ

    2. ๋งž์ถ˜์ฝ”๋“œ

    /*
     ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ(๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„X)
    */
    
    import java.io.IOException;
    
    public class bj1157{
    	public static void main(String[] args) throws IOException{
    	
    		int[] arr = new int[26]; // ์˜๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋Š” 26๊ฐœ
     
    		int c = System.in.read();
     
    		while (c > 64) {	// ๊ณต๋ฐฑ์„ ์ž…๋ ฅ๋ฐ›๋Š” ์ˆœ๊ฐ„ ์ข…๋ฃŒ
    			
    			if (c < 91) {
    				arr[c - 65]++;
    			} else {
    				arr[c - 97]++;
    			}
    			c = System.in.read();
    		}
     
     
    		int max = -1;
    		int ch = -2;	// ? ๋Š” 63
     
    		for (int i = 0; i < 26; i++) {
     
    			if (arr[i] > max) {
    				max = arr[i];
    				ch = i;
    			} else if (arr[i] == max) {
    				ch = -2;
    			}
    		}
    		System.out.print((char) (ch+65));
    
    	}
    }

    3. ํ‹€๋ฆฐ์ฝ”๋“œ: ์‹œ๊ฐ„์ดˆ๊ณผ

    /*
     ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ(๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„X)
    */
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class bj1157{
    	public static void main(String[] args) throws IOException{
    	
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    		String s = br.readLine().toUpperCase();
    		char[] txtMax = new char[]{s.charAt(0),s.charAt(0)};
    		int[] cMax = new int[]{0,0};
    		int count;
    		
    		for (int i=0; i<s.length(); i++)
    		{
    			count = 0;
    			
    			if(i!=0 && txtMax[0] == s.charAt(i))
    			{
    				continue;
    			}
    			
    
    			for(int j=i; j<s.length(); j++)
    			{
    				if(s.charAt(i)==s.charAt(j))
    				{
    					count++;
    				}
    				
    			}
    
    			if(cMax[0]<count)	
    			{
    				txtMax[0]	= s.charAt(i);
    				cMax[0]		= count;
    			}else if(cMax[0]==count)
    			{
    				txtMax[1]	= s.charAt(i);
    				cMax[1]		= count;
    			}
    		}
    
    		if(cMax[0] == cMax[1])
    		{
    			System.out.println("?");
    		}else{
    			System.out.println(txtMax[0]);
    		}
    
    	}
    }

    4. ์ฐธ์กฐ

    https://st-lab.tistory.com/64