Last active
October 15, 2021 14:51
-
-
Save fabiommendes/bfde9ed1e399238e8c3a416661897513 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| /** | |
| * Imprime um array de inteiros. | |
| */ | |
| void print_array_int(long long *lst, long long size) | |
| { | |
| printf("{"); | |
| // Itera sobre todos os elementos, menos o último | |
| for (long long i = 0; i < size - 1; i++) | |
| { | |
| printf("%lld, ", lst[i]); | |
| } | |
| // Imprime o último elemento, se existir | |
| if (size != 0) | |
| { | |
| printf("%lld", lst[size - 1]); | |
| } | |
| printf("}"); | |
| } | |
| /** | |
| * Inicializa a array com n elementos da sequência de Fibonacci | |
| */ | |
| void fibonacci(long long *fibs, long long n) | |
| { | |
| // Python: | |
| // fibs = [1, 1] | |
| // for _ in range(8): | |
| // fibs.append(fibs[-1] + fib[-2]) | |
| // i = 2 | |
| // {1, 1, 2, 3, ?, ?, ?, ?, ?, ?} | |
| // i: ^ | |
| // i + 1: ^ | |
| // i + 2: ^ | |
| fibs[0] = 1; | |
| fibs[1] = 1; | |
| for (long long i = 0; i < n - 2; i++) | |
| { | |
| fibs[i + 2] = fibs[i] + fibs[i + 1]; | |
| } | |
| } | |
| /** | |
| * Soma todos os elementos de um array de tamanho n | |
| */ | |
| long long sum(long long *lst, long long n) | |
| { | |
| long long soma = 0; | |
| for (long long i = 0; i < n; i++) | |
| { | |
| soma += lst[i]; | |
| } | |
| return soma; | |
| } | |
| void main() | |
| { | |
| long long n; | |
| printf("short: %d, int %d, long: %d, long long: %d\n", | |
| sizeof(short), | |
| sizeof(int), | |
| sizeof(long), | |
| sizeof(long long)); | |
| printf("n: "); | |
| scanf("%lld", &n); | |
| long long fibs[n]; | |
| fibonacci(fibs, n); | |
| print_array_int(fibs, n); | |
| printf("\nsoma: %lld\n", sum(fibs, n)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment