-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_5.c
62 lines (59 loc) · 1.43 KB
/
8_5.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
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
typedef unsigned long long ull;
void reverse(unsigned char *a) {
unsigned char tmp = *a, res = 0;
for (short i = 0; i < 7; i++) {
res = res | (tmp & 1);
res = res << 1;
tmp = tmp >> 1;
}
res = res | (tmp & 1);
*a = res;
}
int main() {
FILE *input = fopen("input.txt", "rb");
FILE *output = fopen("output.txt", "wb");
unsigned int n, l, cnt = 0;
unsigned char tmp;
fread(&n, sizeof(unsigned int), 1, input);
ull *array = (ull *)calloc(sizeof(ull), n);
unsigned char *nums = (unsigned char *)calloc(sizeof(unsigned char), n);
for (int i = 0; i < n; i++) {
fread(&l, sizeof(unsigned int), 1, input);
nums[i] = l;
for (int j = 0; j < l; j++) {
fread(&tmp, sizeof(char), 1, input);
array[i] = array[i] << 1;
array[i] = array[i] | tmp;
}
}
cnt = 0;
tmp = 0;
fread(&n, sizeof(int), 1, input);
for (int i = 0; i < n; i++) {
fread(&l, 2, 1, input);
for (char j = nums[l] - 1; j >= 0; j--) {
tmp = tmp << 1;
tmp = tmp | ((array[l] >> j) & 1);
cnt += 1;
if (cnt == 8) {
cnt = 0;
reverse(&tmp);
fwrite(&tmp, sizeof(unsigned char), 1, output);
tmp = 0;
}
}
}
if (cnt > 0) {
for (short i = cnt; i < 8; i++) {
tmp = tmp << 1;
}
reverse(&tmp);
fwrite(&tmp, sizeof(char), 1, output);
}
free(array);
free(nums);
fclose(input);
fclose(output);
}