سوال ۶۱ کنکور ارشد علوم کامپیوتر ۱۴۰۲
اهمیت
۰/۵
خروجی شبه کد زیر، از چه مرتبهای است؟
k = 0
for(i = n , i > 0 , i--)
for(j = i, j > 0, j--)
{k = k + i * j}
print (k)
( یعنی یک واحد کمکردن از )
پاسخ صحیح: گزینهٔ D —
پاسخ تشریحی
The question asks for the order of the printed value k, not the running time. Accumulate the contributions:
The inner sum is the triangular number:
Substitute back:
The dominant piece is , while is lower order. Hence
So the magnitude of the output is of order , which is option 4.
Pitfall: The running time of this code is only (the body runs times). The question instead asks about the size of the value
kthat is printed — each addition contributes , pushing the accumulated value to . Read carefully whether a complexity question is about time or about the computed result.
#complexity#nested-loops#summation#growth-rates#big-theta#output-magnitude