add 6 to 8

This commit is contained in:
juan 2021-11-25 17:04:19 +08:00
parent 59dbc4ce6a
commit 92c01818fd
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
3 changed files with 93 additions and 0 deletions

31
pset9/6-swap.c Normal file
View file

@ -0,0 +1,31 @@
#include <stdio.h>
void swap(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]);
}
int tmp;
for (int i = 0; i < n; i++) {
scanf("%d", &tmp);
swap(&arr[i], &arr[tmp]);
}
for (int i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}
printf("%d\n", arr[n - 1]);
return 0;
}
void swap(int *l, int *r) {
int temp = *l;
*l = *r;
*r = temp;
}

31
pset9/7-substring.c Normal file
View file

@ -0,0 +1,31 @@
// I'm cheating lol
#include <stdio.h>
#include <string.h>
#define MAX 2002
int main(void) {
char arr[MAX] = {};
char substr[MAX] = {};
char tmp;
int i = 0;
while ((tmp = getc(stdin)) != '-' && tmp != '\n') {
arr[i++] = tmp;
}
for (i = 0; i < 2; i++) {
getc(stdin);
}
i = 0;
while ((tmp = getc(stdin)) != '-' && tmp != '\n') {
substr[i++] = tmp;
}
/* printf("%s\n%s\n", arr, substr); */
if (strstr(arr, substr) != NULL) {
printf("ListB is the sub sequence of ListA.\n");
} else {
printf("ListB is not the sub sequence of ListA.\n");
}
return 0;
}

31
pset9/8-longest.c Normal file
View file

@ -0,0 +1,31 @@
#include <stdio.h>
#define MAXNUM 1000
int main(void) {
int n;
scanf("%d", &n);
int maxlen[MAXNUM] = {};
int count = 0;
int prev, head;
scanf("%d", &prev);
for (int i = 1; i < n; i++) {
scanf("%d", &head);
if (head == prev) {
maxlen[count]++;
} else {
prev = head;
count++;
}
}
int max = 1;
for (int i = count; i >= 0; i--) {
if (maxlen[i] + 1 > max) {
max = maxlen[i] + 1;
}
}
printf("%d", max);
return 0;
}