https://duua-coding.tistory.com/15

(Lab 7-7)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int nplates[3]; // 탑에 몇개의 원판이 있는지 변수
int tower[3][100]; // 어떤 원판들이 있는지 보관
int count = 0;

//타워의 내용 초기화, start 위치에 n개의 원판(n~1)이 있다.
void init_tower(int n, char start) {
	int i, tower_no;

	//탑의 번호 A, B, C -> 0,1,2
	tower_no = start - 'A'; 

	//tower[][] 내용과 nplates[]를 초기화 한다
	// nplates[]에 원판의 수를 저장함
	for (i = 0; i < 3; i++) {
		nplates[i] = 0;
		if (i == tower_no)
			nplates[i] = n;
	}

	//tower[0][0] ~ [0][3] = 4, 3, 2, 1 식으로 기억시킨다.
	for (i = n; i > 0; i--) { 
		tower[tower_no][n - i] = i;
	}
}

//탑의 현재 상태 표시
void print_tower() {
	//tower[][] 내용을 보여준다 
	for (int i = 0; i < 3; i++) {
		printf("%c: ", 'A' + i);
		for (int j = 0; j < nplates[i]; j++)
			printf("%d ", tower[i][j]);
		printf("\n");
	}

	printf("\n");
	printf("다음 Enter :");
	getchar();
	fflush(stdin); // 버퍼 완전히 비우기
}

void move_one(int n, char from, char to) {
	printf("\n원판 %d을 %c에서 %c로 옮긴다.\n", n, from, to);
	// nplates[] 와 tower[][] 내용을 수정한다
	// from에서는 제일 위의 원판을 빼고
	// to 에는 제일 끝에 원판을 추가하고
	// nplates[] 값ㅇ르 감소/증가 한다.
	// 이동이 발생할 때 마다 print_tower() 호출
	int f = from - 'A';
	int t = to - 'A';
	tower[t][nplates[t]] = tower[f][nplates[f] - 1];
	nplates[t]++;
	nplates[f]--;
	count++;

	print_tower();
}

void hanoi_tower(int n, char from, char tmp, char to) {
	if (n == 1)
		move_one(1, from, to);
	else {
		hanoi_tower(n - 1, from, to, tmp);
		move_one(n, from, to);
		hanoi_tower(n-1, tmp, from, to);
		
	}
}


void main() {
	init_tower(4, 'A');
	printf("초기 상태\n");
	print_tower();
	hanoi_tower(4, 'A', 'B', 'C');
	printf("원판을 모두 옮겼습니다. \n");
	printf("원판을 옮긴 횟수: %d\n", count);
	
}

(Lab7-ACM1)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>


void find_fibos(int n);
int find_max_fibo(int n);

void main() {
	int nT, num;

	scanf("%d", &nT);
	for (int i = 0; i < nT; i++) {
		scanf("%d", &num);
		find_fibos(num);
	}

	
}

void find_fibos(int n) {
	if (n == 1) {
		printf("%d ", n);
		return;
	}
	else {
		int f;
		f = find_max_fibo(n);
		if (f == n) {
			printf("%d ", f);
			return;
		}
		else {
			find_fibos(n - f);
			printf("%d ", f);
		}
	}
}

int find_max_fibo(int n) {
	int n0 = 0, n1 = 1, n2 = n0 + n1;
	
	if (n == 0) return n0;
	else if (n == 1) return n1;
	else {
		while (n2 <= n) {
			n0 = n1;
			n1 = n2;
			n2 = n0 + n1;
		}
		return n1;
	}
}

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int fibo(int n) {
	int a, b, c;
	a = 0;
	b = 1;

	if (n <= 1) {
		return n;
	}

	else {
		for (int i = 2; i <= n; i++) {
			c = a + b;
			a = b;
			b = c;

		}
		return c;
	}
	
}

void print_fibo(int* p, int count) {
	for (int i = count; i >= 0; i--)
		printf("%d ", p[i]);
	printf("\n");
}

void main() {
	
	int t, n, sum, j;
	int size = 10, count;
	int* p;

	scanf("%d", &t);
	for (int i = 0; i < t; i++) {
		sum = 0;
		count = 0;
		size = 10;
		p = (int*)malloc(sizeof(int) * size);
		scanf("%d", &n);

		while (sum != n) {
			j = 0;
			while (fibo(j) <= (n - sum) )
				j++;

			j--;
			if (count >= size) {
				size *= 2;
				p = (int*)realloc(p, sizeof(int)*size);
			}
			p[count++] = fibo(j);
			sum += fibo(j);
		}
		print_fibo(p, count-1);
	}
	
}

 


(Lab 7- ACM2(1))

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void find_henry(int a, int b);

int main() {
	int nT, a, b;
	scanf("%d", &nT);
	for (int i = 0; i < nT; i++) {
		scanf("%d %d", &a, &b);
		find_henry(a, b);
	}
}



void find_henry(int a, int b) {
	if (a == 1) {
		printf("%d\n", b);
		return;
	}
	else {
		int x = (b + a - 1) / a; 
		a = a * x - b;
		b = b * x;
		find_henry(a, b);
	}
}

 


(Lab 7- ACM2(2))

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

void find_henry(int a, int b);

int main() {
	int nT, a, b;
	scanf("%d", &nT);
	for (int i = 0; i < nT; i++) {
		scanf("%d %d", &a, &b);
		printf("%d/%d = ", a, b);
		find_henry(a, b);

	}
}

void find_henry(int a, int b) {
	if (a == 1) {
		printf("1/%d\n", b);
		return;
	}
	else {
		int x = (b + a - 1) / a;
		printf("1/%d + ", x);
		a = a * x - b;
		b = b * x;
		find_henry(a, b);
	}
}

'프로그래밍랩' 카테고리의 다른 글

기말고사 기출  (0) 2024.11.29
[프로그래밍랩] 14주차, 난수  (1) 2024.11.26
프로그래밍랩 12주차  (0) 2024.11.19
프로그래밍랩 10-11 주차  (0) 2024.11.08
프로그래밍랩 9주차  (0) 2024.11.07

+ Recent posts