Codeforces 문제 푸는 길 - 236A Boy or Girl

2993 단어 Codeforces
A. Boy or Girl
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network.
But yesterday, he came to see "her"in the real world and found out "she"is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users' genders by their user names.
This is his method: if the number of distinct characters in one's user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help our hero to determine the gender of this user by his method.
Input
The first line contains a non-empty string, that contains only lowercase English letters — the user name. This string contains at most 100 letters.
Output
If it is a female by our hero's method, print "CHAT WITH HER!"(without the quotes), otherwise, print "IGNORE HIM!"(without the quotes).
Examples
input
wjmzbmr

output
CHAT WITH HER!

input
xiaodao

output
IGNORE HIM!

input
sevenkplus

output
CHAT WITH HER!

Note
For the first example. There are 6 distinct characters in "wjmzbmr". These characters are: "w", "j", "m", "z", "b", "r". So wjmzbmr is a female and you should print "CHAT WITH HER!".
문제풀이 사고방식: 본 문제는 문자열에 나타난 문자 유형의 수량이 홀수일 경우male이고 그렇지 않으면female이라는 뜻이다.문제를 풀 때 Set 집합이 중복되지 않기 때문에 set만 필요합니다.크기 ()는 이 문자열의 문자 형식의 수를 알 수 있습니다.
알림: "wjmzbmr"에는 6글자만 있습니다.length=7이지만 m이 한 번 반복되었기 때문에 6글자만 있습니다. 그래서 "CHAT WITH HER!"를 출력합니다.
다음은 문제풀이 코드(java 구현)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		String str = scanner.nextLine();
		char[] ch = str.toCharArray();
		Set set = new HashSet(); 
		for(int i = 0;i < ch.length;i++){
			set.add(ch[i]);
		}
		if(set.size() % 2 == 0){
			System.out.println("CHAT WITH HER!");
		}else{
			System.out.println("IGNORE HIM!");
		}	
		scanner.close();
	}
}

좋은 웹페이지 즐겨찾기