-
Notifications
You must be signed in to change notification settings - Fork 310
/
ch-1.c
53 lines (49 loc) · 1.01 KB
/
ch-1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool check(int x[], int y[], int xlen) {
int cnt = 0;
for (int i = 0; i < xlen; i++)
if (x[i] == y[i])
cnt++;
return ( cnt == xlen );
}
void sort(int x[], int xlen) {
int cnt = 0;
do {
cnt = 0;
for (int i = 0; i < xlen - 1; i++) {
if (x[i] > x[i+1]) {
cnt++;
int tmp = x[i];
x[i] = x[i+1];
x[i+1] = tmp;
}
}
} while (cnt != 0);
}
void proc(int x[], int y[], int xlen) {
printf("Input: x = ");
for (int i = 0; i < xlen; i++)
printf("%d ", x[i]);
printf("\n\ty = ");
for (int i = 0; i < xlen; i++)
printf("%d ", y[i]);
printf("\nOutput: ");
sort(x, xlen);
sort(y, xlen);
for (int i = -99; i < 99; i++) {
int* n = malloc(xlen * sizeof(int));
for (int j = 0; j < xlen; j++)
n[j] += x[j] + i;
if (check(n, y, xlen))
printf("%d\n", i);
free(n);
}
}
int main() {
int x[] = {3, 7, 5};
int y[] = {9, 5, 7};
int xlen = sizeof(x) / sizeof(int);
proc(x, y, xlen);
}