Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #define STEP 128
- void checkAlloc(void *p) {
- if (p == NULL) {
- exit(-1);
- }
- }
- typedef struct {
- int len, resMem, *at;
- } DynArr;
- DynArr mkArr(int ini) {
- assert(ini >= 0);
- DynArr arr = {ini, ini, NULL};
- if (ini) {
- arr.at = calloc(sizeof(int), ini);
- checkAlloc(arr.at);
- }
- return arr;
- }
- void addToArr(DynArr *arr, int x) {
- if (arr->len == arr->resMem) {
- arr->resMem += STEP;
- arr->at = realloc(arr->at, sizeof(int) * arr->resMem);
- checkAlloc(arr->at);
- }
- arr->at[(arr->len)++] = x;
- }
- DynArr readArr(int *n) {
- scanf("%d", n);
- DynArr arr = mkArr(*n);
- for (int i = 0; i < *n; i++) {
- scanf("%d", &(arr.at[i]));
- }
- return arr;
- }
- void printArr(const DynArr *const arr) {
- for (int i = 0; i < arr->len; i++) {
- printf("%d ", arr->at[i]);
- }
- puts("");
- }
- int cond(int x) {
- return x & 1;
- }
- DynArr intercl(DynArr a, DynArr b) {
- int i = 0, j = 0;
- DynArr res = mkArr(0);
- while (i < a.len && j < b.len) {
- int newElem;
- if (a.at[i] == b.at[j]) {
- newElem = a.at[i];
- i++, j++;
- } else if (a.at[i] < b.at[j]) {
- newElem = a.at[i];
- i++;
- } else {
- newElem = b.at[j];
- j++;
- }
- if (cond(newElem) && (!res.len || res.at[res.len - 1] != newElem)) {
- addToArr(&res, newElem);
- }
- }
- for (;i < a.len; i++) {
- if (cond(a.at[i]) && (!res.len || res.at[res.len - 1] != a.at[i])) {
- addToArr(&res, a.at[i]);
- }
- }
- for (;j < b.len; j++) {
- if (cond(b.at[j]) && (!res.len || res.at[res.len - 1] != b.at[j])) {
- addToArr(&res, b.at[j]);
- }
- }
- return res;
- }
- int main(void) {
- int n, m;
- DynArr a = readArr(&n);
- DynArr b = readArr(&m);
- DynArr res = intercl(a, b);
- printArr(&res);
- free(a.at);
- free(b.at);//*/
- free(res.at);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement