ncrabs++;
}
- int mindist = INT_MAX;
- for (int i = minp; i<=maxp; i++) {
- int dist = 0;
+ int lastdist = INT_MAX;
+ int dist = INT_MAX;
+ int i = minp;
+ do {
+ lastdist = dist;
+ dist = 0;
for (int j = 0; j<ncrabs; j++) {
int d = abs(crabs[j]-i);
- for (int k = 1; k<=d; k++) {
+ for (int k = 1; k<=d; k++)
dist += k;
- }
}
- if (dist < mindist)
- mindist = dist;
- }
- printf("%d\n", mindist);
+ i++;
+ } while (dist < lastdist);
+ printf("%d\n", lastdist);
}