-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeads.c
137 lines (120 loc) · 2.7 KB
/
beads.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
ID: talos
LANG: C
PROG: beads
*/
#include <stdio.h>
#define INPUT "beads.in"
#define OUTPUT "beads.out"
#define BLUE 98
#define RED 114
#define WHITE 119
#define MAX 700
main ()
{
/* definitions */
unsigned short int n, c1, c2, cur, flag, lastcolor, point ,sum, result;
char beads[MAX];
FILE *in, *out;
/* initialization */
result = 0;
/* read input file to a string*/
in = fopen (INPUT, "r");
fscanf (in, "%d", &n);
fscanf (in, "%s", beads);
/* open file for output data */
out = fopen (OUTPUT, "w");
/* if no red or blue exists , then result=n */
for (c1 = 0, flag = 0; c1 < n; c1++)
{
cur = (int) beads[c1];
if (cur == BLUE)
{
flag++;
break;
}
}
for (c1 = 0; c1 < n; c1++)
{
cur = (int) beads[c1];
if (cur == RED)
{
flag++;
break;
}
}
if (flag != 2)
{
result = n;
fprintf (out, "%d\n", result);
fclose (out);
return 0;
}
/* double the array*/
for (c1 = 0; c1 < n; c1++)
{
beads[c1 + n] = beads[c1];
}
for (c1 = 0, sum = 0, lastcolor = 0, flag = 0; c1 < 2 * n; c1++)
{
cur = beads[c1];
if (cur == WHITE)
{
sum++;
continue ;
}
if (cur == BLUE)
{
sum++;
if (lastcolor == 2) /* it was red*/
{
flag++;
point= c1;
}
lastcolor = 1; /* it is blue */
if (flag == 2)
{
/* end of circle*/
sum--; flag=0 ; lastcolor=0;
if (sum>result)
{
result=sum ;
}
/* call last pointer*/
c1 = point ; sum=0;
}
continue ;
}
if (cur == RED)
{
sum++;
if (lastcolor == 1) /* it was blue*/
{
flag++;
point= c1;
}
lastcolor = 2; /* it is red */
if (flag == 2)
{
/* end of circle*/
sum--; flag=0; lastcolor=0;
if (sum>result)
{
result=sum ;
}
/* call last pointer*/
c1 = point ; sum=0;
}
}
}
if (result>n)
{
result=n;
}
/* print result to file*/
fprintf (out, "%d\n", result);
/* close files and exit*/
fclose (out);
fclose (in);
return (0);
}