함수ì�˜ ì¤‘ë³µì •ì�˜ë�¼ 함ì�€
함수ì�˜ ì�´ë¦„ì�€ 같게 하ë�˜ ì „ë‹¬ì�¸ìž�들ì�˜ 개수나 형ì‹�ì—� ë”°ë�¼ 다르게 사용ë� 수 있ë�„ë¡� ì •ì�˜í•˜ëŠ”ê²ƒì�„ ë§�함..
즉.. �어� 한 단어� 뜻� 문맥�황� 따� 다르게 해��는것� 뜻함
- 마방진 짜본것.. 아�� 많� 미숙하네요..
~cpp
#include <iostream>
using namespace std;
int const arsize = 11;
void main()
{
int num, garo=0, sero=0, cnt=1;
cout << " ì›�하는 마방진ì�˜ í�¬ê¸°ë¥¼ ìž…ë ¥í•˜ì„¸ìš” (11 ì�´í•˜ì�˜ 홀수만) : ";
cin >> num;
int square[arsize][arsize]={0,};
sero = num/2;
square[0][sero]=1;
while ( cnt < num*num )
{
garo--;
sero++;
if (garo < 0)
garo = num-1;
if (sero > num-1)
sero = 0;
if (square[garo][sero] !=0)
{ garo+=2, sero-=1;}
if (garo > num-1)
garo = 1;
if (sero < 0)
sero = num-1;
square[garo][sero]=++cnt;
}
for (int i = 0 ; i <num; i++)
{
for ( int j = 0 ; j < num ; j++)
{
cout << square[i][j] << "\t";
}
cout << endl;
}
}
~cpp
#include <iostream>
using namespace std;
const int max=20;
void main()
{
int cnt=0, board[max][max]={0,};
int garo,sero,x,y;
char direction[max];
cout << "íŒ�ì�˜ í�¬ê¸°ë¥¼ ìž…ë ¥í•˜ì„¸ìš” (í–‰, ì—´) : ";
cin >> garo >> sero;
cout << "처ì�Œ 시작 위치를 ìž…ë ¥í•˜ì„¸ìš” (í–‰, ì—´) : ";
cin >> x >> y;
board[x][y]=1;
cout << "ë°©í–¥ì�„ ìž…ë ¥í•˜ì„¸ìš” (0ì�€ ë¶�쪽방향 , 시계 방향으로 1~7 , ë��내기는 999) : ";
for (int i=0; i<max; i++)
{
cin >> direction[i];
cnt++;
if(direction[i-2]=='9' && direction[i-1]=='9' && direction[i]=='9')
{
cnt-=3;
break;
}
}
for (int k=0; k<cnt; k++)
{
switch(direction[k])
{
case '0' : x--;
break;
case '1' : x--;
y++;
break;
case '2' : y++;
break;
case '3' : x++;
y++;
break;
case '4' : x++;
break;
case '5' : x++;
y--;
break;
case '6' : y--;
break;
case '7' : x--;
y--;
break;
}
if(x==garo)
x=0;
else if(x<0)
x=garo-1;
if(y==sero)
y=0;
else if(y<0)
y=sero-1;
board[x][y]++;
}
for (int j=0 ; j<garo ; j++)
{
for (i=0 ; i<sero ; i++)
cout << board[j][i] << " ";
cout << endl ;
}
cout << endl << "� �� 횟수는 " << cnt << "입니다. ";
}
~cpp
#include <iostream>
using namespace std;
const int max=100;
void input(int &a, int &b);
void input(short &a, short &b);
void input(char array[], int &cnt);
void process(char array[], int board[][max], int garo, int sero, int cnt, int x, int y);
void output(int garo, int sero, int cnt, int board[][max]);
void main()
{
int cnt=0, board[max][max]={0,};
int garo,sero;
short x,y;
char direction[max];
input(garo,sero);
input(x,y);
board[x][y]=1;
input(direction ,cnt);
process(direction, board, garo, sero, cnt, x, y);
output(garo,sero,cnt,board);
}
void input(int &a, int &b)
{
cout << "íŒ�ì�˜ í�¬ê¸°ë¥¼ ìž…ë ¥í•˜ì„¸ìš” (í–‰, ì—´) : ";
cin >> a >> b;
}
void input(short &a, short &b)
{
cout << "처ì�Œ 시작 위치를 ìž…ë ¥í•˜ì„¸ìš” (í–‰, ì—´) : ";
cin >> a >> b;
}
void input(char array[] , int &cnt)
{
cout << "ë°©í–¥ì�„ ìž…ë ¥í•˜ì„¸ìš” (0ì�€ ë¶�쪽방향 , 시계 방향으로 1~7 , ë��내기는 999) : ";
for (int i=0; i<max; i++)
{
cin >> array[i];
cnt++;
if(array[i-2]=='9' && array[i-1]=='9' && array[i]=='9')
{
cnt-=3;
break;
}
}
}
void process(char array[], int board[][max], int garo, int sero, int cnt, int x, int y)
{
for (int k=0; k<cnt; k++)
{
switch(array[k])
{
case '0' : x--;
break;
case '1' : x--;
y++;
break;
case '2' : y++;
break;
case '3' : x++;
y++;
break;
case '4' : x++;
break;
case '5' : x++;
y--;
break;
case '6' : y--;
break;
case '7' : x--;
y--;
break;
}
if(x==garo)
x=0;
else if(x<0)
x=garo-1;
if(y==sero)
y=0;
else if(y<0)
y=sero-1;
board[x][y]++;
}
}
void output(int garo, int sero, int cnt, int board[][max])
{
for (int j=0 ; j<garo ; j++)
{
for (int i=0 ; i<sero ; i++)
cout << board[j][i] << " ";
cout << endl ;
}
cout << endl << "� �� 횟수는 " << cnt << "입니다. ";
}










