From 235484859ebf77d129534903cbad0959d0a61bbc Mon Sep 17 00:00:00 2001 From: juan Date: Mon, 1 Nov 2021 20:25:24 +0800 Subject: [PATCH] add 1 to 4 --- pset6/1-soldiers.c | 34 ++++++++++++++++++++++++++++++++++ pset6/2-circle.c | 22 ++++++++++++++++++++++ pset6/3-reverse.c | 43 +++++++++++++++++++++++++++++++++++++++++++ pset6/4-binary.c | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 pset6/1-soldiers.c create mode 100644 pset6/2-circle.c create mode 100644 pset6/3-reverse.c create mode 100644 pset6/4-binary.c diff --git a/pset6/1-soldiers.c b/pset6/1-soldiers.c new file mode 100644 index 0000000..11171b1 --- /dev/null +++ b/pset6/1-soldiers.c @@ -0,0 +1,34 @@ +#include + +int main(void) { + // Initialize variables + int n; + scanf("%d", &n); + + // Start looping + int sum; + int found = 0; + for (int a = 0; found != 1; a++) { + sum = a * 5 + 1; + for (int b = 0; sum >= b * 6 + 5; b++) { + if (sum != b * 6 + 5) { // The sum don't equal + continue; + } + for (int c = 0; sum >= 7 * c + 6; c++) { + if (sum != 7 * c + 6) { + continue; + } + for (int d = 0; sum >= 11 * d + 10; d++) { + if (sum != 11 * d + 10) { + continue; + } + if (sum >= n) { + printf("%i\n", sum); + found = 1; + } + } + } + } + } + return 0; +} diff --git a/pset6/2-circle.c b/pset6/2-circle.c new file mode 100644 index 0000000..7b07e5d --- /dev/null +++ b/pset6/2-circle.c @@ -0,0 +1,22 @@ +#include + +// Function prototypes +int calculate(int a); + +int main(void) { + // Initialize variables + int n; + scanf("%i", &n); + + // Calculate answer + printf("%i\n", calculate(n)); + return 0; +} + +int calculate(int a) { + if (a == 0) { + return 1; + } else { + return calculate(a - 1) - a + a * 2; + } +} diff --git a/pset6/3-reverse.c b/pset6/3-reverse.c new file mode 100644 index 0000000..f9c8dc9 --- /dev/null +++ b/pset6/3-reverse.c @@ -0,0 +1,43 @@ +#include + +// Function prototypes +int get_length(int n); +int get_last(int n, int loc); + +int main(void) { + // Initialize variables + int n; + scanf("%d", &n); + + int len = get_length(n); + /* printf("%i\n", len); */ + for (int i = 1; i <= len / 2; i++) { + if (get_last(n, len - i + 1) != get_last(n, i)) { + /* printf("the number at %i is %i\n", i, get_last(n, i)); */ + /* printf("the number at %i is %i\n", len - i, get_last(n, len - i + 1)); */ + printf("No\n"); + return 0; + } + } + printf("Yes\n"); + return 0; +} + +int get_length(int n) { + int len = 0; + if (n == 0) { // 0 should have the same digits as 1 - 9 + n = 9; + } + while (n > 0) { + n /= 10; + len++; + } + return len; +} + +int get_last(int n, int loc) { + for (int i = 1; i < loc; i++) { + n /= 10; + } + return n % 10; +} diff --git a/pset6/4-binary.c b/pset6/4-binary.c new file mode 100644 index 0000000..6081cf2 --- /dev/null +++ b/pset6/4-binary.c @@ -0,0 +1,33 @@ +#include + +// Function prototypes +int bin2dec(int n); +int pow2(int n); + +int main(void) { + // Initialize variables + int n; + scanf("%d", &n); + + printf("%d\n", bin2dec(n)); + return 0; +} + +int bin2dec(int n) { + int ans = 0; + int loc = 0; + while (n != 0) { + ans += pow2(loc) * (n % 10); + n /= 10; + loc++; + } + return ans; +} + +int pow2(int n) { + int ans = 1; + for (int i = 0; i < n; i++) { + ans *= 2; + } + return ans; +}