마방진 알로리즘을 설명하자면 <div><br /></div> <div>1) 첫 행, 중간 열에 1을 집어 넣습니다. 예컨데 5x5행렬에서 1행 3열에 1을 집어넣습니다.</div> <div><br /></div> <div>2) 그 다음 한 칸 위, 좌로 다음 수 2(n)을 집어 넣으며 이 단계를 반복합니다.</div> <div><br /></div> <div>3) 2번 단계를 반복할 때, 만약 그 자리에 숫자가 저장되어 있다면 자리를 차지한 바로 밑 행열에 수를 저장합니다.</div> <div><br /></div> <div>요런 알고리즘이 정의되어있을 때</div> <div><br /></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><font style="background-color: #ffffff">void formula(int number)<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" />{<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> int x=0, y;</font></strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><font style="background-color: #ffffff"><br /></font></strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><font style="background-color: #ffffff"> // 행을 담당한 변수 x, 열을 담당한 변수 y를 정의합니다<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" />// 이때 x는 0으로 초기화하여 1행에서 시작됩니다.</font></strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><font style="background-color: #ffffff"><br /></font></strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><font style="background-color: #ffffff"> y=number/2; </font></strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px">// 1행 중간열을 정의하기위해, y의 (number/2)로 정의합니다. </strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><font style="background-color: #ffffff"> for(int i=1;i<=number*number;i++) // i는 1에서 행렬의 갯수 nub*nub까지 반복합니다.<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> {<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> box[x][y]=i; // 1~x행,1~y열에 1~nub*nub를 대입합니다.<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> if(i%number==0) <br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> {<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> x=x+1;<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> }<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> else<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> {<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> x=x-1;<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> y=y+1;<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> }<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> if(x<0)<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> {<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> x=number-1;<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> }<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> if(y>number-1)<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> {<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> y=0;<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> }<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" /> }<br style="margin: 0px; padding: 0px; border: none; line-height: 1.3" />}</font></strong></div> <div><br /></div> <div>혼자선 도저히 코딩이 안되서</div> <div><br /></div> <div>안되서 인터넷에 널린 코드를 보면서햇는데도</div> <div><br /></div> <div>이해르 못하고... </div> <div><br /></div> <div>안되겠다 싶어서 저가 직접 주석 달았는데...</div> <div><br /></div> <div><br /></div> <div>문제는 조건식<font face="돋움, Dotum, AppleGothic, sans-serif" size="2"><span style="line-height: 17px"><b>.... </b></span></font></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><br /></strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px">if(i%number==0) </strong></div> <div><strong style="margin: 0px; padding: 0px; border: none; line-height: 17px; font-family: 돋움, Dotum, AppleGothic, sans-serif; font-size: 13px"><br /></strong></div> <div><font face="돋움, Dotum, AppleGothic, sans-serif" size="2"><span style="line-height: 17px"><b>이게 핵심 코드인데...</b></span></font></div> <div><font face="돋움, Dotum, AppleGothic, sans-serif" size="2"><span style="line-height: 17px"><b><br /></b></span></font></div> <div><font face="돋움, Dotum, AppleGothic, sans-serif" size="2"><span style="line-height: 17px"><b>i를 nub로 나눠 나눈 나머지가 0일 때...</b></span></font></div> <div><font face="돋움, Dotum, AppleGothic, sans-serif" size="2"><span style="line-height: 17px"><b><br /></b></span></font></div> <div><font face="돋움, Dotum, AppleGothic, sans-serif" size="2"><span style="line-height: 17px"><b>행이 증가하는 건데..</b></span></font></div> <div><font face="돋움, Dotum, AppleGothic, sans-serif" size="2"><span style="line-height: 17px"><b><br /></b></span></font></div> <div><div>요게 무슨 뜻인지 이해를 못하겠네요...</div> <div><br /></div> <div>요거 좀 쉽게 풀이해주시면 안될까요...</div></div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.