From 59dbc4ce6ad71497130952631b8c9f0f78e9effb Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 25 Nov 2021 16:13:12 +0800 Subject: [PATCH] add 1 to 5 --- pset9/1-avg.c | 15 +++++++++++ pset9/2-search-linear.c | 51 +++++++++++++++++++++++++++++++++++ pset9/2-search.c | 60 +++++++++++++++++++++++++++++++++++++++++ pset9/3-sort.c | 35 ++++++++++++++++++++++++ pset9/4-vector.c | 25 +++++++++++++++++ pset9/5-greedy.c | 32 ++++++++++++++++++++++ 6 files changed, 218 insertions(+) create mode 100644 pset9/1-avg.c create mode 100644 pset9/2-search-linear.c create mode 100644 pset9/2-search.c create mode 100644 pset9/3-sort.c create mode 100644 pset9/4-vector.c create mode 100644 pset9/5-greedy.c diff --git a/pset9/1-avg.c b/pset9/1-avg.c new file mode 100644 index 0000000..e6e3810 --- /dev/null +++ b/pset9/1-avg.c @@ -0,0 +1,15 @@ +#include + +int main(void) { + int n; + scanf("%i", &n); + double arr[n]; + for (int i = 0; i < n; i++) { + scanf("%lf", &arr[i]); + } + double sum = 0; + for (int i = 0; i < n; i++) { + sum += arr[i]; + } + printf("%.2lf\n", sum / n); +} diff --git a/pset9/2-search-linear.c b/pset9/2-search-linear.c new file mode 100644 index 0000000..fff9403 --- /dev/null +++ b/pset9/2-search-linear.c @@ -0,0 +1,51 @@ +#include +#include + +int search(const int *arr, const int size, const int query); + +int main(void) { + int n; + scanf("%d", &n); + int *arr = malloc(sizeof(int) * n); + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + int m; + int tmp; + int loc; + scanf("%d", &m); + for (int i = 0; i < m; i++) { + scanf("%d", &tmp); + loc = search(arr, n, tmp); + if (loc == -1) { + printf("NULL\n"); + } else { + if (loc - 1 >= 0) { + if (loc + 1 < n) { + printf("%d %d\n", arr[loc - 1], arr[loc + 1]); + } else { + printf("%d\n", arr[loc - 1]); + } + } else { + if (loc + 1 < n) { + printf("%d\n", arr[loc + 1]); + } else { + printf("NULL\n"); + } + } + } + } + + free(arr); + return 0; +} + +int search(const int *arr, const int size, const int query) { + for (int i = 0; i < size; i++) { + if (arr[i] == query) { + return i; + } + } + return -1; +} diff --git a/pset9/2-search.c b/pset9/2-search.c new file mode 100644 index 0000000..f78eb6e --- /dev/null +++ b/pset9/2-search.c @@ -0,0 +1,60 @@ +#include + +int maxnum = -2147483648; + +void mkhashtable(const int *arr, const int size, int *table); +void printNum(const int *arr, const int loc, const int size); + +int main(void) { + int n; + scanf("%d", &n); + int arr[n]; + // Initialize array + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + if (arr[i] > maxnum) + maxnum = arr[i]; + } + + int hash[maxnum]; + mkhashtable(arr, n, hash); + + // Search + int m; + scanf("%d", &m); + int tmp; + for (int i = 0; i < m; i++) { + scanf("%d", &tmp); + if (tmp < 0 || tmp >= maxnum) { // Not in the hash table + printf("NULL\n"); + } else { + printNum(arr, hash[tmp], n); + } + } + return 0; +} + +void mkhashtable(const int *arr, const int size, int *table) { + // Initialize + for (int i = 0; i < maxnum; i++) { + table[i] = -1; + } + // Fill values + for (int i = 0; i < size; i++) { + table[arr[i]] = i; + } + + return; +} + +void printNum(const int *arr, const int loc, const int size) { + if (loc == -1) { + printf("NULL\n"); + } else { + if (loc - 1 >= 0) + printf("%d ", arr[loc - 1]); + if (loc + 1 <= size) + printf("%d ", arr[loc + 1]); + printf("\n"); + } +} diff --git a/pset9/3-sort.c b/pset9/3-sort.c new file mode 100644 index 0000000..4171102 --- /dev/null +++ b/pset9/3-sort.c @@ -0,0 +1,35 @@ +#include + +void swap(int *arr, int l, int r); + +int main(void) { + int n; + scanf("%d", &n); + + int arr[n]; + for (int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + + /* swap(arr, 0, 1); */ + // Bubble sort + for (int i = 0; i < n; i++) { + for (int j = 0; j < n - 1; j++) { + if (arr[j] % 2 == 1 && arr[j + 1] % 2 == 0) { + swap(arr, j, j + 1); + } + } + } + + for (int i = 0; i < n - 1; i++) { + printf("%d ", arr[i]); + } + printf("%d\n", arr[n - 1]); + return 0; +} + +void swap(int *arr, int l, int r) { + int tmp = arr[l]; + arr[l] = arr[r]; + arr[r] = tmp; +} diff --git a/pset9/4-vector.c b/pset9/4-vector.c new file mode 100644 index 0000000..787c2a0 --- /dev/null +++ b/pset9/4-vector.c @@ -0,0 +1,25 @@ +#include + +int main(void) { + int n; + scanf("%d", &n); + int vec1[n]; + int vec2[n]; + + for (int i = 0; i < 2; i++) { + for (int j = 0; j < n; j++) { + if (i == 0) + scanf("%d", &vec1[j]); + else + scanf("%d", &vec2[j]); + } + } + + int sum = 0; + for (int i = 0; i < n; i++) { + sum += vec1[i] * vec2[i]; + } + + printf("%d\n", sum); + return 0; +} diff --git a/pset9/5-greedy.c b/pset9/5-greedy.c new file mode 100644 index 0000000..c91476d --- /dev/null +++ b/pset9/5-greedy.c @@ -0,0 +1,32 @@ +#include + +int getCash(int n); + +int main(void) { + int n; + scanf("%d", &n); + + getCash(n); + return 0; +} + +int getCash(int n) { + int cashes[7] = {}; + int cash_rep[7] = {100, 50, 20, 10, 5, 2, 1}; + + while (n) { + for (int i = 0; i < 7; i++) { + if (n >= cash_rep[i]) { + n -= cash_rep[i]; + cashes[i]++; + break; + } + } + } + + for (int i = 0; i < 7; i++) { + if (cashes[i]) + printf("%d:%d\n", cash_rep[i], cashes[i]); + } + return 1; +}