Logo C++ Online Judge

C++

TA:
统计

We want an integer midpoint of two values l and r, with l <= r. Here, the required answer is their exact mathematical average with any fractional part discarded toward zero. For nonnegative array indices, this means rounding the average down.

Consider these four expressions:

/* A */ (l + r) / 2
/* B */ l + (r - l) / 2
/* C */ ((long long)l + r) / 2
/* D */ l / 2 + r / 2 + (l % 2 + r % 2) / 2

Both variables have type int. Assume a 32-bit signed int, from -2147483648 through 2147483647, and a 64-bit signed long long. The cast in C converts l before the addition, so that addition is performed as long long.

Integer division discards the fractional part toward zero. The remainder satisfies x = (x / 2) * 2 + x % 2; it can be negative when x is negative. A signed operation that exceeds its type’s range has undefined behavior.

Midpoints are used, for example, to divide a range of array indices into two parts. The questions below specify which values l and r can take.

提交

Written answers receive AI rubric assessment.

Up to 3 submission attempts per student. Sign in to see your remaining attempts.

Each submission includes all questions. Review your answers before submitting.

Short explanations are welcome. You may leave answers blank; unanswered questions receive zero credit.

1. Select every expression that gives the required midpoint for all array indices with 0 <= l <= r <= 2147483647. (20 points)Select all correct choices. Credit requires an exact match.
2. Explain your selection. You may group expressions that rely on the same idea; a separate proof for every expression is unnecessary. (50 points)
3. A different program needs the same midpoint definition for any ordered pair of int values, including negative values. Choose one expression from A–D that you would use for every such pair and justify it. You need not classify the other three. (30 points)
Drafts are saved in this browser every 30 seconds.