▶ 줄기 잎 그림이란 통계학에서 통계적 자료를 표 형태와 그래프 형태의 혼합된 방법을 나타내는 것을 말합니다.
 예를들어 다음과 같은 키(cm)의 통계적 자료가 있습니다.
 187 189  164  175  189  166  195  196  159  186 

위 자료를 줄기 잎 그림 방법을 통해 나타내면,
 줄기    잎 
 15    9
 16    4 6
 17    5
 18    6 7 9 9
 19    5 6

이 방법의 특징은 공통되는 부분을 모은 열이 줄기이고, 그 나머지 부분을 모은 열이 잎입니다.

이 것을 간단하게 프로그래밍으로 구현해보겠습니다. 

Tree.txt


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Tree {

	public static void main(String[] args) {

		ArrayList stem = new ArrayList();
		String temp = "";
		Scanner s = new Scanner(System.in);
		
		System.out.println("데이터의 수를 입력하십시오.");
		int count = s.nextInt();
		int cm = 0;
		System.out.println("키(cm)를 입력하십시오.");
		for (int i = 0; i < count; i++) {
			cm = s.nextInt();
			stem.add(cm);
		}

		Collections.sort(stem);
		System.out.println(stem);
		for (int i = 0; i < count; i++) {
			temp += stem.get(i).toString().subSequence(2, 3) + " ";
			
			if(i==(count-1)) {
				System.out.println(stem.get(i).toString().substring(0, 2) + " : " + temp);
				break;
			}
			
			if (!(stem.get(i).toString().substring(0, 2).equals(stem.get(i + 1).toString().substring(0, 2)))) {
				System.out.println(stem.get(i).toString().substring(0, 2) + " : " + temp);
				temp = "";
			}
		}
	}
}


 실행 결과 




posted by 쪼재