add 1 to 5

This commit is contained in:
juan 2021-11-25 16:13:12 +08:00
parent a876da7ea6
commit 59dbc4ce6a
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
6 changed files with 218 additions and 0 deletions

15
pset9/1-avg.c Normal file
View file

@ -0,0 +1,15 @@
#include <stdio.h>
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);
}

51
pset9/2-search-linear.c Normal file
View file

@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

60
pset9/2-search.c Normal file
View file

@ -0,0 +1,60 @@
#include <stdio.h>
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");
}
}

35
pset9/3-sort.c Normal file
View file

@ -0,0 +1,35 @@
#include <stdio.h>
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;
}

25
pset9/4-vector.c Normal file
View file

@ -0,0 +1,25 @@
#include <stdio.h>
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;
}

32
pset9/5-greedy.c Normal file
View file

@ -0,0 +1,32 @@
#include <stdio.h>
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;
}