Portál AbcLinuxu, 26. října 2025 14:08
int groups = 0;
/* whichever comes first */
while ( (i < magnets) && (!feof(fp)) )
{
fgets(buf, BUFMAX, fp);
i++;
/* input validation */
if ( ((buf[0] == '+') && (buf[1] == '-')) || ( (buf[0] == '-') && (buf[1] == '+')) ) {
/* first magnet in queue */
if (last_pole == 'x') {
last_pole = buf[1];
groups = 1;
continue;
}
if (buf[0] == last_pole) {
groups++;
}
last_pole = buf[1];
}
else {
fprintf (stderr, "ERROR: invalid entry! Skipping this line.. \n");
continue;
}
}
fprintf (stdout, "%d\n", groups);
int
main (void)
{
int n, res = 1;
char x, y, yprev;
if (scanf("%d", &n) != 1)
goto err;
if (scanf(" %c%c", &x, &yprev) != 2)
goto err;
for (; --n; yprev = y)
{
if (scanf(" %c%c", &x, &y) != 2)
goto err;
if (yprev != y)
res++;
}
printf("%d\n", res);
return EXIT_SUCCESS;
err:
puts("Invalid input");
return EXIT_FAILURE;
}
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.