-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.c
90 lines (81 loc) · 2.12 KB
/
scan.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <math.h>
int main()
{
int n, start, dir, temp;
int startpos, dist = 0;
printf("Enter the no of tasks to be seeked");
scanf("%d", &n);
int arr[n];
printf("Enter the tasks");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the starting posn");
scanf("%d", &start);
startpos = start;
printf("Enter the direction to proceed 1 for R and 2 for L\n");
scanf("%d", &dir);
// sort the sequence
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
// sorted
if (dir == 1)
{
for (int i = 0; i < n; i++)
{
if (arr[i] >= startpos)
{
dist = dist + abs(startpos - arr[i]);
startpos = arr[i];
printf("%d ", arr[i]);
}
}
dist = dist + abs(startpos - 199);
startpos = 199;
for (int i = n - 1; i >= 0; i--)
{
if (arr[i] < startpos && arr[i] <= start)
{
dist = dist + abs(startpos - arr[i]);
printf("%d ", arr[i]);
startpos = arr[i];
}
}
}
if (dir == 2)
{
for (int i = n - 1; i >= 0; i--)
{
if (arr[i] <= startpos)
{
dist = dist + abs(startpos - arr[i]);
printf("%d ", arr[i]);
startpos = arr[i];
}
}
dist = dist + abs(startpos - 0);
startpos = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] >= startpos && arr[i] > start)
{
dist = dist + abs(startpos - arr[i]);
startpos = arr[i];
printf("%d ", arr[i]);
}
}
}
printf("%d", dist);
}