Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
952 B
1
Indexable
Never
#include <stdio.h>

int f(int n) {
	int total = 0, bottle = 0, borrow = 0, flag = 0;
	if (n % 3 == 1) {
		bottle += 2;
		borrow = 2;
	}
	else if (n % 3 == 2) {
		bottle += 1;
		borrow = 1;
	}
	else {
		flag = 1;
	}

	while (1) {
		int temp;
		total += n;
		if (n + bottle == borrow) {
			break;
		}
		else if (flag && n == 1) {
			break;
		}
		temp = bottle;
		bottle = (n + bottle) % 3;
		n = (n + temp) / 3;
	}
	return total;
}

void main() {
	char inputLine[100];
	int result[100];
	int resultLength = 0;

	if (fgets(inputLine, sizeof(inputLine), stdin) != NULL)
	{
		char* lineTheRest = inputLine;
		int offset;
		while (sscanf(lineTheRest, " %d%n", &result[resultLength], &offset) == 1)
		{
			resultLength++;
			lineTheRest += offset;
		}
	}

	for (int i = 1; i < result[0] + 1; i++) {
		if (i == 1) {
			printf("%d", f(result[i]));
		}
		else {
			printf(" %d", f(result[i]));
		}
	}
}