|
| 1 | +#include <assert.h> |
| 2 | +#include <limits.h> |
| 3 | +#include <math.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stddef.h> |
| 6 | +#include <stdint.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +char* readline(); |
| 12 | + |
| 13 | +// Complete the utopianTree function below. |
| 14 | +int utopianTree(int n) |
| 15 | +{ |
| 16 | + int sum=1; |
| 17 | + for(int i=1;i<=n;i++) |
| 18 | + { |
| 19 | + if(i%2==0) |
| 20 | + { |
| 21 | + sum=sum+1; |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + sum=sum*2; |
| 26 | + } |
| 27 | + } |
| 28 | + return sum; |
| 29 | +} |
| 30 | + |
| 31 | +int main() |
| 32 | +{ |
| 33 | + FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); |
| 34 | + |
| 35 | + char* t_endptr; |
| 36 | + char* t_str = readline(); |
| 37 | + int t = strtol(t_str, &t_endptr, 10); |
| 38 | + |
| 39 | + if (t_endptr == t_str || *t_endptr != '\0') { exit(EXIT_FAILURE); } |
| 40 | + |
| 41 | + for (int t_itr = 0; t_itr < t; t_itr++) { |
| 42 | + char* n_endptr; |
| 43 | + char* n_str = readline(); |
| 44 | + int n = strtol(n_str, &n_endptr, 10); |
| 45 | + |
| 46 | + if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } |
| 47 | + |
| 48 | + int result = utopianTree(n); |
| 49 | + |
| 50 | + fprintf(fptr, "%d\n", result); |
| 51 | + } |
| 52 | + |
| 53 | + fclose(fptr); |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +char* readline() { |
| 59 | + size_t alloc_length = 1024; |
| 60 | + size_t data_length = 0; |
| 61 | + char* data = malloc(alloc_length); |
| 62 | + |
| 63 | + while (true) { |
| 64 | + char* cursor = data + data_length; |
| 65 | + char* line = fgets(cursor, alloc_length - data_length, stdin); |
| 66 | + |
| 67 | + if (!line) { break; } |
| 68 | + |
| 69 | + data_length += strlen(cursor); |
| 70 | + |
| 71 | + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } |
| 72 | + |
| 73 | + size_t new_length = alloc_length << 1; |
| 74 | + data = realloc(data, new_length); |
| 75 | + |
| 76 | + if (!data) { break; } |
| 77 | + |
| 78 | + alloc_length = new_length; |
| 79 | + } |
| 80 | + |
| 81 | + if (data[data_length - 1] == '\n') { |
| 82 | + data[data_length - 1] = '\0'; |
| 83 | + } |
| 84 | + |
| 85 | + data = realloc(data, data_length); |
| 86 | + |
| 87 | + return data; |
| 88 | +} |
0 commit comments