U E D R , A S I H C RSS

Bee Maja/고준영

. .
.. --;;;;;;

~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)로 계산하지 말고 미리 계산된 값을 변수에 저장해놓고 비교하는게 더 좋을듯. - 고준영
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:35
Processing time 0.0147 sec