U E D R , A S I H C RSS

Jolly Jumpers/iruril

JollyJumpers.java

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

public class JollyJumpers {
	
	int [] jumpersArray;
	int length;
	int differenceValue;
	boolean [] differenceArray;
	boolean jolly;
	
	// input()은 getIntArray()에서 사용
	public String input()
	{
		BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
		String input = "";
		try {
			input = in.readLine();
		}
		catch(IOException e) {
			e.printStackTrace();			
		}
		return input;
	}

	public int [] getIntArray()
	{
		String buf = input();
		String [] stringArray = buf.split(" ");
		
		// 배열의 길이를 구한다(n)
		length = stringArray.length;
		
		int [] intArray = new int [length];
		
		for(int i = 0; i < length; i++ )
		{
			intArray[i] = Integer.parseInt(stringArray[i]);
		}	
		return intArray;
	}
	
	public void inputJumpers()
	{
		// int형 배열을 얻는다
		jumpersArray = getIntArray();

	}

	public void checkDiffenceValue()
	{
		// 두 수의 차 값은 1 ~ n-1
		differenceValue = length-1;
//		System.out.println(differenceValue);

		// checkDifferArray[] 를  초기화한다
		differenceArray = new boolean [length];
		differenceArray =  setFalse( differenceArray );

		int tempDiffer;	
		for(int i = 0; i < differenceValue; i++)
		{
			tempDiffer = Math.abs( jumpersArray[i] - jumpersArray[i+1] );
//			System.out.println(tempDiffer);
			if( tempDiffer < length)
				differenceArray[tempDiffer] = true; 
		}
	}
	
	public boolean checkJolly()
	{
		jolly = true;
		for(int i = 1; i <= differenceValue; i++)
		{
			if ( differenceArray[i] == false )
			{
				jolly = false;
				break;
			}
		}		
		if(jolly)
			System.out.println("Jolly");		
		else
			System.out.println("Not Jolly");		
		
		return jolly;
	}
	
	public boolean [] setFalse( boolean [] temp )
	{
		for(int i = 0; i < temp.length; i++)
		{
			temp[i] = false;
		}
		return temp;
	}
	
	
	
	public static void main(String args[])
	{
		while(true)
		{
			JollyJumpers jj = new JollyJumpers();
			jj.inputJumpers();
			jj.checkDiffenceValue();
			
			if(jj.jumpersArray[0] == 0)
				break;
			jj.checkJolly();
		}
	}
}
  • MineSweeper/황재선 코드를 참고한 다음 코딩했습니다
  • 입력 부분에서 차음에 배열의 길이를 입력받지 않습니다
    ex ) 4 1 4 3 1 (x) 1 4 3 1 (o)

Thread

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:35
Processing time 0.0140 sec