CODEMALAYALAM

kangaroo solution

Jul 14th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  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.     char** split_string(char*);
  13.  
  14.     // Complete the kangaroo function below.
  15.  
  16.     // Please either make the string static or allocate on the heap. For example,
  17.     // static char str[] = "hello world";
  18.     // return str;
  19.     //
  20.     // OR
  21.     //
  22.     // char* str = "hello world";
  23.     // return str;
  24.     //
  25.     char* kangaroo(int x1, int v1, int x2, int v2) {
  26.  
  27.     char *yes = "YES";
  28.     char *no = "NO";
  29.    
  30.     if(v1 == v2) return no;
  31.    
  32.     int n = (x1 - x2 + v2 - v1)/(v2 - v1);
  33.     int m = (x1 - x2 + v2 - v1)%(v2 - v1);
  34.    
  35.     if((n>0) && (m==0))
  36.         return yes;
  37.     else
  38.         return no;
  39.        
  40.     }
  41.                
  42.     int main()
  43.     {
  44.         FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
  45.    
  46.         char** x1V1X2V2 = split_string(readline());
  47.  
  48.         char* x1_endptr;
  49.         char* x1_str = x1V1X2V2[0];
  50.         int x1 = strtol(x1_str, &x1_endptr, 10);
  51.  
  52.         if (x1_endptr == x1_str || *x1_endptr != '\0') { exit(EXIT_FAILURE); }
  53.  
  54.         char* v1_endptr;
  55.         char* v1_str = x1V1X2V2[1];
  56.         int v1 = strtol(v1_str, &v1_endptr, 10);
  57.  
  58.         if (v1_endptr == v1_str || *v1_endptr != '\0') { exit(EXIT_FAILURE); }
  59.  
  60.         char* x2_endptr;
  61.         char* x2_str = x1V1X2V2[2];
  62.         int x2 = strtol(x2_str, &x2_endptr, 10);
  63.  
  64.         if (x2_endptr == x2_str || *x2_endptr != '\0') { exit(EXIT_FAILURE); }
  65.  
  66.         char* v2_endptr;
  67.         char* v2_str = x1V1X2V2[3];
  68.         int v2 = strtol(v2_str, &v2_endptr, 10);
  69.  
  70.         if (v2_endptr == v2_str || *v2_endptr != '\0') { exit(EXIT_FAILURE); }
  71.  
  72.         char* result = kangaroo(x1, v1, x2, v2);
  73.  
  74.         fprintf(fptr, "%s\n", result);
  75.  
  76.         fclose(fptr);
  77.  
  78.         return 0;
  79.     }
  80.  
  81.     char* readline() {
  82.         size_t alloc_length = 1024;
  83.         size_t data_length = 0;
  84.         char* data = malloc(alloc_length);
  85.  
  86.         while (true) {
  87.             char* cursor = data + data_length;
  88.             char* line = fgets(cursor, alloc_length - data_length, stdin);
  89.  
  90.             if (!line) { break; }
  91.  
  92.             data_length += strlen(cursor);
  93.  
  94.             if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
  95.  
  96.             size_t new_length = alloc_length << 1;
  97.             data = realloc(data, new_length);
  98.  
  99.             if (!data) { break; }
  100.  
  101.             alloc_length = new_length;
  102.         }
  103.  
  104.         if (data[data_length - 1] == '\n') {
  105.             data[data_length - 1] = '\0';
  106.         }
  107.  
  108.         data = realloc(data, data_length);
  109.  
  110.         return data;
  111.     }
  112.  
  113.     char** split_string(char* str) {
  114.         char** splits = NULL;
  115.         char* token = strtok(str, " ");
  116.  
  117.         int spaces = 0;
  118.  
  119.         while (token) {
  120.             splits = realloc(splits, sizeof(char*) * ++spaces);
  121.             if (!splits) {
  122.          return splits;
  123.             }
  124.  
  125.             splits[spaces - 1] = token;
  126.  
  127.             token = strtok(NULL, " ");
  128.         }
  129.  
  130.         return splits;
  131.     }
Add Comment
Please, Sign In to add comment