سوال ۶۱ کنکور ارشد علوم کامپیوتر ۱۴۰۴
فرض کنید یک لیست پیوندی با گره بهصورت زیر باشد.
(گره NO. در فیلد info مقدار و در فیلد link اشارهگر به گره بعدی را دارد.)
قطعه کد زیر را در نظر بگیرید.
x = 0;
cur = L;
while (cur != NULL)
{
cur1 = cur;
while (cur1 != NULL) {
x += cur1 -> info ;
cur1 = cur1 -> link; }
cur = cur -> link;
}
پس از اجرای این قطعه کد مقدار کدام است؟
پاسخ صحیح: گزینهٔ C —
The list holds the values in order. The outer loop walks cur over every node , and for each starting position the inner loop adds up info from node all the way to the last node .
Inner loop from a start position . When cur sits on node , the inner loop accumulates
Total over all start positions. Use the contribution technique: count how many times each value is added. Value is reached by the inner loop exactly when the outer loop starts at some position , i.e. for — that is times. Hence each value contributes , and
This is exactly option 3 (its first term is ).
Sanity check with . Start at node : . Start at node : . Total . Compare: option 3 gives ✓; option 1 gives ✗; option 4 gives ✗.
The closed form equals , but among the choices it appears as the explicit sum of squares.
Exam trick: A nested traversal where the inner loop runs from the outer pointer to the end means item is summed once for every outer start — so it is counted times, turning a plain sum into .