Snippets

TeamCOINSE CAVM/AVMf

Updated by JUNHWI KIM

File triangle.c Added

  • Ignore whitespace
  • Hide word diff
+int get_type(int a, int b, int c) {
+  int type;
+
+  if (a > b) {
+    int t = a;
+    a = b;
+    b = t;
+  }
+  if (a > c) {
+    int t = a;
+    a = c;
+    c = t;
+  }
+  if (b > c) {
+    int t = b;
+    b = c;
+    c = t;
+  }
+
+  if (a + b <= c) {
+    type = 0;
+  } else {
+    type = 1;
+    if (a == b) {
+      if (b == c) {
+        type = 3;
+      }
+    } else {
+      if (a == b) {
+        type = 4;
+      } else if (b == c) {
+        type = 4;
+      }
+    }
+  }
+
+  return type;
+}
Updated by JUNHWI KIM

File line.c Added

  • Ignore whitespace
  • Hide word diff
+struct Line {
+  double x1;
+  double y1;
+  double x2;
+  double y2;
+};
+
+int intersect(struct Line a, struct Line b) {
+  double ua_t = (b.x2 - b.x1) * (a.y1 - b.y1) - (b.y2 - b.y1) * (a.x1 - b.x1);
+  double ub_t = (a.x2 - a.x1) * (a.y1 - b.y1) - (a.y2 - a.y1) * (a.x1 - b.x1);
+  double u_b = (b.y2 - b.y1) * (a.x2 - a.x1) - (b.x2 - b.x1) * (a.y2 - a.y1);
+
+  if (u_b != 0) {
+    double ua = ua_t / u_b;
+    double ub = ub_t / u_b;
+
+    if (0 <= ua) {
+      if (ua <= 1) {
+        if (0 <= ub) {
+          if (ub <= 1) {
+            return 1;
+          }
+        }
+      }
+    }
+    return 0;
+
+  } else {
+    if (ua_t == 0) {
+      return 1;
+    }
+
+    if (ub_t == 0) {
+      return 1;
+    }
+
+    return 0;
+  }
+}
Updated by JUNHWI KIM

File allzero.c Added

  • Ignore whitespace
  • Hide word diff
+#include <stdio.h>
+int allzero(int* list) {
+  int size = 7;
+
+  int total = 0, i;
+  for (i = 0; i < size; i++) {
+    printf("%d, %d\n", i, list[i]);
+    if (list[i] == 0) {
+      total++;
+    }
+  }
+  if (total == size) {
+    printf("All zeros\n");
+  }
+  return 0;
+}
Created by JUNHWI KIM

File calendar.c Added

  • Ignore whitespace
  • Hide word diff
+int isLeapYear(int year) {
+  return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
+}
+
+int monthDays(int month, int year) {
+  int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+  return month == 2 && isLeapYear(year) ? 29 : month_days[month - 1];
+}
+
+int daysBetween(int start_month, int start_day, int start_year, int end_month,
+                int end_day, int end_year) {
+  int days = 0;
+
+  // sanitize month inputs
+  if (start_month < 1) start_month = 1;
+  if (end_month < 1) end_month = 1;
+  if (start_month > 12) start_month = 12;
+  if (end_month > 12) end_month = 12;
+
+  // sanitize day inputs
+  if (start_day < 1) start_day = 1;
+  if (end_day < 1) end_day = 1;
+  if (start_day > monthDays(start_month, start_year))
+    start_day = monthDays(start_month, start_year);
+  if (end_day > monthDays(end_month, end_year))
+    end_day = monthDays(end_month, end_year);
+
+  // swap dates if start date before end date
+  int swapDates = 0;
+  if (end_year < start_year) {
+    swapDates = 1;
+  }
+  if (end_year == start_year) {
+    if (end_month < start_month) {
+      swapDates = 1;
+    }
+  }
+  if (end_year == start_year) {
+    if (end_month == start_month) {
+      if (end_day < start_day) {
+        swapDates = 1;
+      }
+    }
+  }
+  if (swapDates) {
+    int t = end_month;
+    end_month = start_month;
+    start_month = t;
+    t = end_day;
+    end_day = start_day;
+    start_day = t;
+    t = end_year;
+    end_year = start_year;
+    start_year = t;
+  }
+
+  // calculate days
+  if (start_month == end_month) {
+    if (start_year == end_year) {
+      days = end_day - start_day;
+    }
+  } else {
+    days += monthDays(start_month, start_year) - start_day;
+    days += end_day;
+    if (start_year == end_year) {
+      int month = start_month + 1;
+      while (month < end_month) {
+        days += monthDays(month, start_year);
+        month++;
+      }
+    } else {
+      int year;
+      int month = start_month + 1;
+      while (month <= 12) {
+        days += monthDays(month, start_year);
+        month++;
+      }
+      month = 1;
+      while (month < end_month) {
+        days += monthDays(month, end_year);
+        month++;
+      }
+      year = start_year + 1;
+      while (year < end_year) {
+        days += 365;
+        if (isLeapYear(year)) days++;
+        year++;
+      }
+    }
+  }
+
+  return days;
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.