인사이트에서 진행하고 있는 이벤트에 응모하기 위한 코드입니다. 
문제는 http://www.insightbook.co.kr/post/3814 에 있습니다. 

제가 작성한 코드는 다음과 같습니다.


package insight;

import java.util.Arrays;

public class InsightQuiz {

	public static void main(String[] args) {
		InsightQuiz quiz=new InsightQuiz();
		int before[]={1,2,3,4,5,6,7,8,9,10,11,12};
		
		for(int k=-1;k<20;k++) {
			System.out.println("k="+k);
			quiz.changeArrayPosition(before, k);
			System.out.println("-----------------------------------");
		}
	}
	public int[] changeArrayPosition(int[] before,int k) {
		//If k value is lower than 0, this print error message and return null.
		if(k<0) {
			System.out.println("k value should be over than 0.");
			return null;
		}
		
		int arrayLength=before.length;
		// k value can bigger than arrayLength. So reset k value with mod operator. 
		k=k%arrayLength;
		int []after=new int[arrayLength];
		
		if(k % arrayLength==0) {
			//if mod value of k and arrayLength is 0, it is same. 
			after=before;
		} else {
			for(int loop=0;loop<arrayLength;loop++) {
				int newPos=loop+k;
				if(newPos>=arrayLength) {
					newPos=newPos-arrayLength;
				}
				after[newPos]=before[loop];
			}
		}
		System.out.println(Arrays.toString(before));
		System.out.println(Arrays.toString(after));
		return after;
	}
}




:

카테고리

분류 전체보기 (1)
InsightEvent (1)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

태그목록

달력

«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

글 보관함