그저 내가 되었고

☕️Java:: 백준 자바 11319번 본문

개발/Java

☕️Java:: 백준 자바 11319번

hyuunii 2023. 8. 10. 23:11

Count Me In 성공다국어

 
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 256 MB 1405 949 875 69.944%

 

 

문제

Given a sentence in English, output the counts of consonants and vowels.

Vowels are letters in [’A’,’E’,’I’,’O’,’U’,’a’,’e’,’i’,’o’,’u’].

 

 

입력

The test file starts with an integer S(1 ≤ S ≤ 100), the number of sentences.

Then follow S lines, each containing a sentence - words of length 1 to 20 separated by spaces. Every sentence will contain at least one word and be comprised only of characters [a-z][A-Z] and spaces. No sentence will be longer than 1000 characters.

 

 

출력

For each sentence, output the number of consonants and vowels on a line, separated by space.

 

 

예제

입력

3
You can win this thing
May be too optimistic
Just try to have fun

출력 

12 6
10 8
11 5

 

 

 

코딩

이와 같은 코딩은 JS 알고리즘을 풀 때 많이 접했던 문제다.

자바라고 아이디어가 다른건 없으니 겁먹지 말자.

 

1. 인풋을 받는데

2. 걔는 자/모음만 구분하면 되고 대소문자는 상관이 없으니 아예 소문자로 다 바꿔주고 시작할거고

3. trim()함수 이용해서 문장 앞 뒤의 공백 제거

4. replaceAll()로 문장 중간의 공백들 제거

5. 모음이 갯수가 적으니 걔를 기준으로(if) 갯수 세어주고, 나머지(else) 자음 갯수를 세준다

6. 갯수가 둘 다 0이 아닐경우 출력해준다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
 
        int n = sc.nextInt();
 
        for (int i = 0; i <= n; i++) {
            String input = sc.nextLine().toLowerCase().trim().replaceAll(" ","");
 
            int vowels = 0;
            int consonants = 0;
            for(int j = 0; j < input.length(); j++) {
                if(input.charAt(j)=='a'||input.charAt(j)=='e'||input.charAt(j)=='i'||input.charAt(j)=='o'||input.charAt(j)=='u') {
                    vowels++;
                } else{
                    consonants++;
                }
            }
            if (vowels != 0 && consonants !=0){
                System.out.println(consonants+" "+vowels);
            }
        }
    }
}
cs

 

 

약간 수정한 버전

위의 6번에서... 둘 다 0이 아닐때<-이거는 nextInt()와 nextLine()을 함께 쓸 때의 개행문자 처리때문에 저렇게 해준건데

밑에처럼 아예 개행문자를 제거해주고, 그에 따라 for문도 n번만 돌게 했다.

그렇게 마지막에 if문은 필요가 없어져 삭제했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
 
        int n = sc.nextInt();
        String removeEnter = sc.nextLine();
 
        for (int i = 0; i < n; i++) {
            String input = sc.nextLine().toLowerCase().trim().replaceAll(" ",""); ;
 
            int vowels = 0;
            int consonants = 0;
            for(int j = 0; j < input.length(); j++) {
                if(input.charAt(j)=='a'||input.charAt(j)=='e'||input.charAt(j)=='i'||input.charAt(j)=='o'||input.charAt(j)=='u') {
                    vowels++;
                } else{
                    consonants++;
                }
            }
            System.out.println(consonants+" "+vowels);
        }
    }
}
cs