profile image

L o a d i n g . . .

// 문자열에 들어 있는 괄호가 맞는지 검사한다. 괄호는 [], {}, ()를 사용할 수 있으며, 서로 중첩할 수 있다.

 

import java.util.LinkedList;

public class CheckBrace {

	public static void main(String[] args) {
		// 문자열에 들어 있는 괄호가 맞는지 검사한다. 괄호는 [], {}, ()를 사용할 수 있으며, 서로 중첩할 수 있다.
		
		String s = "( 4 + [ 3 + { x - y } / 2 ] ) * 7";
		
		LinkedList<Character> stack = new LinkedList<Character>();
		
		for(int i=0; i<s.length(); i++) {
			char c = s.charAt(i);
			
			if( c== '(' || c=='[' || c=='{') {
				stack.push(c); // auto boxing
				System.out.println( stack );
			}
			
			if ( c== ')' || c == ']' || c == '}') {
				if ( stack.isEmpty() ) {
					System.out.println("Not match...");
					System.exit(1);
				}
				
				char d = stack.pop();
				System.out.println(stack);
				
				if ( ( c== ')' && d!='(') || ( c== '[' && d!=']') || ( c== '{' && d!='}') ) {
					System.out.println("Not match...");
					System.exit(1);
				}
			}
		}
		if(!stack.isEmpty()) {
			System.out.println("Not match...");
			System.exit(1);
		}
		System.out.println("Match!");
	}

}

 

아래의 질문의 답을 구하는 프로그램을 작성하시오.
 3개 동아리에 모두 참여하는 학생 명단
 적어도 1개 이상의 동아리에 참여하는 학생 명단
 soccer 또는 dance에 참여하지만, computer에는 참여하지 않는 학생 명단

 

import java.util.HashSet;

public class ClubMember {

	public static void printMSG(HashSet<String> hash, String msg) {
		System.out.println(msg+": "+hash.size()+"명 ==>"+hash);
	}
	
	public static void HashSetResetAsArray(HashSet<String> hash, String[] arr) {
		hash.clear();
		for( String e : arr ) {
			hash.add(e);
		}
	}
	
	public static void main(String[] args) {
		
		String[] computer = {"ㄷㅊ", "ㄷㄱ", "ㅈㅅ", "ㅅㅎ", "ㅇㄹ", "ㅇㅎ", "ㅇㅁ", "ㅎㅇ", "ㄱㅅ", "ㅁㅈ", "ㄱㅌ"};
		String[] soccer = {"ㅊㅁ", "ㅈㅅ", "ㅎㅅ", "ㅇㄹ", "ㅇㅁ", "ㅎㅈ", "ㅎㄱ", "ㅁㅈ", "ㅈㅇ", "ㄱㅌ", "ㄷㄱ", "ㅈㅅ"};
		String[] dance = {"ㅈㅅ", "ㅅㅎ", "ㅇㄹ", "ㅇㅁ", "ㅊㅎ", "ㅎㄱ", "ㅌㅎ", "ㄱㅌ", "ㅅㅇ", "ㅇㄹ", "ㅎㅇ"};

		HashSet<String> computerHashSet = new HashSet<String>();
		HashSet<String> soccerHashSet = new HashSet<String>();
		HashSet<String> danceHashSet = new HashSet<String>();
		
		HashSetResetAsArray(computerHashSet, computer);
		HashSetResetAsArray(soccerHashSet, soccer);
		HashSetResetAsArray(danceHashSet, dance);
		
		
		// 3개 동아리에 모두 참여하는 학생 명단
		computerHashSet.retainAll(soccerHashSet);
		computerHashSet.retainAll(danceHashSet);
		printMSG(computerHashSet, "3개 동아리에 모두 참여하는 학생 명단");
		HashSetResetAsArray(computerHashSet, computer);
		
		//적어도 1개 이상의 동아리에 참여하는 학생 명단
		computerHashSet.addAll(soccerHashSet);
		computerHashSet.addAll(danceHashSet);
		printMSG(computerHashSet, "적어도 1개 이상의 동아리에 참여하는 학생 명단");
		HashSetResetAsArray(computerHashSet, computer);
		
		// soccer 또는 dance에 참여하지만, computer에는 참여하지 않는 학생 명단
		soccerHashSet.addAll(danceHashSet);
		soccerHashSet.removeAll(computerHashSet);
		printMSG(soccerHashSet, "soccer 또는 dance에 참여하지만, computer에는 참여하지 않는 학생 명단");
	}

}
복사했습니다!