U E D R , A S I H C RSS

Jolly Jumpers/황재선

1. 모든 수를 수열로 생각

1.1. JollyJumpers.java

~cpp 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

/*
 * Created on 2005. 1. 4
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

public class JollyJumpers {
	int [] nums;
	
	public int [] inputNumbers() {
		String message = processKeyInput();
		String [] ch = splitMessage(message);
		return toInt(ch);
	}

	private String[] splitMessage(String message) {
		return message.split(" ");
	}

	private int[] toInt(String [] ch) {
		int len = ch.length;
		nums = new int[len]; 
		for(int i = 0; i < len; i++) {
			nums[i] = Integer.parseInt(ch[i]);
		}
		return nums;
	}

	private String processKeyInput() {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String message = "";
		try {
			message = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return message;
	}

	public int[] getdifferenceValue() {
		int len = nums.length - 1;
		int [] differValue = new int[len];
		
		for(int i = 0; i < len; i++) {
			differValue[i] = Math.abs(nums[i+1] - nums[i]);
		}
		nums = sort(differValue);
		return nums;
	}

	private int[] sort(int[] aNum) {
		for(int i = 0; i < aNum.length; i++)
			for(int j = 0; j < aNum.length; j++)
				if (aNum[i] < aNum[j])
					aNum = swap(aNum, i, j);
		return aNum;
	}

	private int[] swap(int [] aNum, int i, int j) {
		int temp = aNum[i];
		aNum[i] = aNum[j];
		aNum[j] = temp;
		
		return aNum;
	}

	public boolean isJolly() {
		int len = nums.length;
		for(int i = 1; i < len; i++)
			if (i != nums[i-1])
				return false;
			
		return true;
	}

	private void printResult(Vector v) {
		for(int i = 0; i < v.size(); i++) {
			System.out.println(v.get(i));
		}
	}
	static public void main(String [] args) {
		Vector v = new Vector();
		while(true) {
			JollyJumpers j = new JollyJumpers();
			j.inputNumbers();
			if (j.nums[0] == 0) {
				j.printResult(v);
				break;
			}
			j.getdifferenceValue();
			if (j.isJolly()) 	v.add("Jolly");
			else 				v.add("Not jolly");
		}
	}
}

2. 첫번째 수는 수열의 개수

2.1. JollyJumpers.java

~cpp 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

/*
 * Created on 2005. 1. 6.
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

public class JollyJumpers {
	int size;
	int [] numbers;
	
	public String inputNum() {
		String line = "";
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			line = in.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return line;
	}
	
	public int [] stringToInt(String line) {
		String [] ch = line.split(" ");
		
		int len = ch.length - 1;
		numbers = new int[len];
		size = Integer.parseInt(ch[0]);
		for (int i = 0; i < len; i++) {
			numbers[i] = Integer.parseInt(ch[i+1]);
		}
		return numbers;
	}

	public boolean isRightInput() {
		return size == numbers.length ? true : false;
	}
	
	public TreeSet computeGaps() {
		TreeSet set = new TreeSet();
		
		for(int i = 0; i < numbers.length - 1; i++) {
			set.add(new Integer(Math.abs(numbers[i] - numbers[i+1])));
		}
		return set;
	}
	
	public boolean isJolly(TreeSet set) {
		int numOfSeries = size - 1;
		if (set.size() != numOfSeries) {
			return false;
		}
		Iterator i = set.iterator();
		int series = 1;
		
		while (i.hasNext()) {
			Integer num = (Integer) i.next();
			if (num.intValue() != series) {
				return false;
			}
			series++;
		}
		return true;
	}
	
	public void printResult(ArrayList list) {
		for(int i = 0; i < list.size(); i++) {
			System.out.println((String) list.get(i));
		}
	}
	
	public boolean isException() {
		if (size > 3000) {
			System.out.println("3000 초과. 다시 입력하세요");
			return true;
		}
		if (!isRightInput()) {
			System.out.println("수열의 개수가" + size + "과 다릅니다. 다시 입력하세요");
			return true;
		}
		return false;
	}
	
	public static void main(String [] args) {
		ArrayList list = new ArrayList();
		
		while(true) {
			JollyJumpers j = new JollyJumpers();
			String line = j.inputNum();
			j.stringToInt(line);
			if (j.size == 0) {
				j.printResult(list);
				break;
			}
			if (j.isException())
				continue;
			TreeSet set = j.computeGaps();
			if (j.isJolly(set))
				list.add("Jolly");
			else
				list.add("Not jolly");
		}
	}
}

2.2. TestJollyJumpers.java

~cpp 
import junit.framework.TestCase;
/*
 * Created on 2005. 1. 6.
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

public class TestJollyJumpers extends TestCase {
	JollyJumpers j;	
	String line;
	
	public void setUp() {
		j = new JollyJumpers();
		line = "5 1 4 2 -1 6";
		j.numbers = new int[]{1,4,2,-1,6};
		j.size = 5;
	}
	public void testInputNum() {
		j = new JollyJumpers();
		assertEquals("5 1 4 2 -1 6", line);
	}
	
	public void testStringToInt() {
		int [] num = {1,4,2,-1,6};
		assertEquals(num[0], j.stringToInt(line)[0]);
		assertEquals(num[1], j.stringToInt(line)[1]);
	}
	
	public void testIsRightInput() {
		assertEquals(5, j.numbers.length);
		assertEquals(true, j.isRightInput());
	}
	

	public void testIsJolly() {
		assertEquals(false, j.isJolly(j.computeGaps()));
	}

}


3. 쓰레드

1. ~cpp TreeSet 을 사용했다. sort에 log(n)이 소요된다.
2. main이 지저분하다. 함수로 추출하기도 뭐하다.
3. test코드를 다듬었다. 테스트할때마다 콘솔로 입력받는 게 귀찮아서 test클래스에서 메인 클래스의 field를 정의하고 진행했다. 또 다른 테스트를 하기위해서 고쳐야할 부분이 흩어져 있다. 테스트코드의 설계에 관심을 가져야겠다.
-- 재선

----
JollyJumpers
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.0874 sec