-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubleCola.c
52 lines (41 loc) · 1 KB
/
doubleCola.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
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
const char *who_is_next(long long n, size_t length, const char *const a[length]);
int main(void)
{
size_t n;
printf("Number: ");
scanf("%ld", &n);
char *names[n];
for (int i = 0; i < n; i++)
{
names[i] = malloc(10 * sizeof(char));
if (names[i] == NULL)
{
printf("Space cannot be allocated\n");
return 1;
}
printf("String %d: ", i + 1);
scanf("%s", names[i]);
}
long long pos;
printf("Position: ");
scanf("%lld", &pos);
printf("%s is at the position %lld\n", who_is_next(pos, n, names), pos);
for (size_t i = 0; i < n; i++)
{
free(names[i]);
}
return 0;
}
const char *who_is_next(long long n, size_t length, const char *const a[length])
{
int m = 1;
while (m * length < n)
{
n -= m * length;
m *= 2;
}
return a[(n - 1) / m];
}