인사이트에서 진행하고 있는 이벤트에 응모하기 위한 코드입니다.
문제는 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;
}
}

