|
| 1 | +#include <assert.h> |
| 2 | +#include <ctype.h> |
| 3 | +#include <limits.h> |
| 4 | +#include <math.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include <stddef.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +char* readline(); |
| 13 | +char* ltrim(char*); |
| 14 | +char* rtrim(char*); |
| 15 | +char** split_string(char*); |
| 16 | + |
| 17 | +int parse_int(char*); |
| 18 | + |
| 19 | +/* |
| 20 | + * Complete the 'cutTheSticks' function below. |
| 21 | + * |
| 22 | + * The function is expected to return an INTEGER_ARRAY. |
| 23 | + * The function accepts INTEGER_ARRAY arr as parameter. |
| 24 | + */ |
| 25 | + |
| 26 | +/* |
| 27 | + * To return the integer array from the function, you should: |
| 28 | + * - Store the size of the array to be returned in the result_count variable |
| 29 | + * - Allocate the array statically or dynamically |
| 30 | + * |
| 31 | + * For example, |
| 32 | + * int* return_integer_array_using_static_allocation(int* result_count) { |
| 33 | + * *result_count = 5; |
| 34 | + * |
| 35 | + * static int a[5] = {1, 2, 3, 4, 5}; |
| 36 | + * |
| 37 | + * return a; |
| 38 | + * } |
| 39 | + * |
| 40 | + * int* return_integer_array_using_dynamic_allocation(int* result_count) { |
| 41 | + * *result_count = 5; |
| 42 | + * |
| 43 | + * int *a = malloc(5 * sizeof(int)); |
| 44 | + * |
| 45 | + * for (int i = 0; i < 5; i++) { |
| 46 | + * *(a + i) = i + 1; |
| 47 | + * } |
| 48 | + * |
| 49 | + * return a; |
| 50 | + * } |
| 51 | + * |
| 52 | + */ |
| 53 | +int* cutTheSticks(int arr_count, int* arr, int* result_count) { |
| 54 | + int* result = malloc(arr_count * sizeof(int)); // Dynamic memory for result array |
| 55 | + int result_index = 0; |
| 56 | + |
| 57 | + while (true) { |
| 58 | + // Count non-zero sticks |
| 59 | + int non_zero_count = 0; |
| 60 | + int min_stick_length = INT_MAX; |
| 61 | + |
| 62 | + for (int i = 0; i < arr_count; i++) { |
| 63 | + if (arr[i] > 0) { |
| 64 | + non_zero_count++; |
| 65 | + if (arr[i] < min_stick_length) { |
| 66 | + min_stick_length = arr[i]; // Find the smallest stick length |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // If no sticks left, break the loop |
| 72 | + if (non_zero_count == 0) { |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + // Store the count of sticks cut in the current round |
| 77 | + result[result_index++] = non_zero_count; |
| 78 | + |
| 79 | + // Cut the sticks by the smallest stick length |
| 80 | + for (int i = 0; i < arr_count; i++) { |
| 81 | + if (arr[i] > 0) { |
| 82 | + arr[i] -= min_stick_length; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + *result_count = result_index; // Set the total count of rounds |
| 88 | + return result; // Return the result array |
| 89 | +} |
| 90 | + |
| 91 | +int main() |
| 92 | +{ |
| 93 | + FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); |
| 94 | + |
| 95 | + int n = parse_int(ltrim(rtrim(readline()))); |
| 96 | + |
| 97 | + char** arr_temp = split_string(rtrim(readline())); |
| 98 | + |
| 99 | + int* arr = malloc(n * sizeof(int)); |
| 100 | + |
| 101 | + for (int i = 0; i < n; i++) { |
| 102 | + int arr_item = parse_int(*(arr_temp + i)); |
| 103 | + |
| 104 | + *(arr + i) = arr_item; |
| 105 | + } |
| 106 | + |
| 107 | + int result_count; |
| 108 | + int* result = cutTheSticks(n, arr, &result_count); |
| 109 | + |
| 110 | + for (int i = 0; i < result_count; i++) { |
| 111 | + fprintf(fptr, "%d", *(result + i)); |
| 112 | + |
| 113 | + if (i != result_count - 1) { |
| 114 | + fprintf(fptr, "\n"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fprintf(fptr, "\n"); |
| 119 | + |
| 120 | + fclose(fptr); |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
| 124 | + |
| 125 | +char* readline() { |
| 126 | + size_t alloc_length = 1024; |
| 127 | + size_t data_length = 0; |
| 128 | + |
| 129 | + char* data = malloc(alloc_length); |
| 130 | + |
| 131 | + while (true) { |
| 132 | + char* cursor = data + data_length; |
| 133 | + char* line = fgets(cursor, alloc_length - data_length, stdin); |
| 134 | + |
| 135 | + if (!line) { |
| 136 | + break; |
| 137 | + } |
| 138 | + |
| 139 | + data_length += strlen(cursor); |
| 140 | + |
| 141 | + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + alloc_length <<= 1; |
| 146 | + |
| 147 | + data = realloc(data, alloc_length); |
| 148 | + |
| 149 | + if (!data) { |
| 150 | + data = '\0'; |
| 151 | + |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (data[data_length - 1] == '\n') { |
| 157 | + data[data_length - 1] = '\0'; |
| 158 | + |
| 159 | + data = realloc(data, data_length); |
| 160 | + |
| 161 | + if (!data) { |
| 162 | + data = '\0'; |
| 163 | + } |
| 164 | + } else { |
| 165 | + data = realloc(data, data_length + 1); |
| 166 | + |
| 167 | + if (!data) { |
| 168 | + data = '\0'; |
| 169 | + } else { |
| 170 | + data[data_length] = '\0'; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return data; |
| 175 | +} |
| 176 | + |
| 177 | +char* ltrim(char* str) { |
| 178 | + if (!str) { |
| 179 | + return '\0'; |
| 180 | + } |
| 181 | + |
| 182 | + if (!*str) { |
| 183 | + return str; |
| 184 | + } |
| 185 | + |
| 186 | + while (*str != '\0' && isspace(*str)) { |
| 187 | + str++; |
| 188 | + } |
| 189 | + |
| 190 | + return str; |
| 191 | +} |
| 192 | + |
| 193 | +char* rtrim(char* str) { |
| 194 | + if (!str) { |
| 195 | + return '\0'; |
| 196 | + } |
| 197 | + |
| 198 | + if (!*str) { |
| 199 | + return str; |
| 200 | + } |
| 201 | + |
| 202 | + char* end = str + strlen(str) - 1; |
| 203 | + |
| 204 | + while (end >= str && isspace(*end)) { |
| 205 | + end--; |
| 206 | + } |
| 207 | + |
| 208 | + *(end + 1) = '\0'; |
| 209 | + |
| 210 | + return str; |
| 211 | +} |
| 212 | + |
| 213 | +char** split_string(char* str) { |
| 214 | + char** splits = NULL; |
| 215 | + char* token = strtok(str, " "); |
| 216 | + |
| 217 | + int spaces = 0; |
| 218 | + |
| 219 | + while (token) { |
| 220 | + splits = realloc(splits, sizeof(char*) * ++spaces); |
| 221 | + |
| 222 | + if (!splits) { |
| 223 | + return splits; |
| 224 | + } |
| 225 | + |
| 226 | + splits[spaces - 1] = token; |
| 227 | + |
| 228 | + token = strtok(NULL, " "); |
| 229 | + } |
| 230 | + |
| 231 | + return splits; |
| 232 | +} |
| 233 | + |
| 234 | +int parse_int(char* str) { |
| 235 | + char* endptr; |
| 236 | + int value = strtol(str, &endptr, 10); |
| 237 | + |
| 238 | + if (endptr == str || *endptr != '\0') { |
| 239 | + exit(EXIT_FAILURE); |
| 240 | + } |
| 241 | + |
| 242 | + return value; |
| 243 | +} |
0 commit comments