소감 ¶
마음에 들지 않는다. 아무런 계획없이 무턱데고 코딩부터 했더니 이래저래 지저분게 되었다.
아무래도 나말고 다른사람은 절대로 알아볼수 없을듯.. --;;;;;;
아무래도 나말고 다른사람은 절대로 알아볼수 없을듯.. --;;;;;;
최초 버전 ¶
~c
#include <stdio.h>
#include <stdlib.h>
#define	NORTH 1
#define	NORTH_WEST 2
#define	SOUTH_WEST 3
#define	SOUTH 4
#define	SOUTH_EAST 5
#define CAL(x) ((3*x*x)+(3*x)+1)
struct coordinate{
	int x;
	int y;
} posi;
void move_posi(struct coordinate *, int);
int main(void)
{
	int willy, row, seq, i;
	
	printf("윌리의 좌표계를 입력하세요 : ");
	while(scanf("%d", &willy)){
		if(!willy) exit(0);
		posi.x = 0; posi.y = 0;
		for (row=0; CAL(row) < willy; row++) move_posi(&posi, SOUTH_EAST);
	
		willy -= CAL(row);
		for(seq=NORTH; willy; seq++)
			for(i=0; i < row && willy; i++, willy++) move_posi(&posi, seq);
	
		if(willy == 0)
		printf("(%d, %d)\n\n", posi.x, posi.y);
		printf("윌리의 좌표계를 입력하세요 : ");
	}
	return 0;	
}
void move_posi(struct coordinate *posi, int direc)
{
	switch(direc)
	{
	case NORTH:
		posi->y--;
		break;
	case NORTH_WEST:
		posi->x--;
		break;
	case SOUTH_WEST:
		posi->x--;
		posi->y++;
		break;
	case SOUTH:
		posi->y++;
		break;
	case SOUTH_EAST:
		posi->x++;
		break;
	default:
		puts("정상적인 작동이 아니므로 종료합니다.");
		exit(1);
	}
}
코멘트 ¶
좀더 좋은 방향이 있으시면 한수 가르쳐 주세요
for (row=0; CAL(row) < willy; row++)에서 매번 for문이 돌때마다 CAL(row)로 계산하지 말고 미리 계산된 값을 변수에 저장해놓고 비교하는게 더 좋을듯. - 고준영
for (row=0; CAL(row) < willy; row++)에서 매번 for문이 돌때마다 CAL(row)로 계산하지 말고 미리 계산된 값을 변수에 저장해놓고 비교하는게 더 좋을듯. - 고준영













