📜  门| GATE-CS-2014-(Set-3)|问题21

📅  最后修改于: 2021-06-29 01:12:07             🧑  作者: Mango

仅使用一个临时变量,对于给定的X值,评估多项式P(X)= X 5 + 4X 3 + 6X + 5所需的最小算术运算数。
(A) 6
(B) 7
(C) 8
(D) 9答案: (B)
解释:

P(X) = x5 + 4x3 + 6x + 5

     =x ( x4 + 4x2 + 6 ) +5

     =x ( x ( x3 + 4x ) + 6 ) + 5

     =x ( x ( x ( x2 + 4 ) ) + 6 ) + 5

     =x ( x ( x (x (x) + 4 ) ) + 6 ) + 5

Let T be a temporary variable to store intermediate results.

1. T = (x) * (x)
2. T = T + 4
3. T = (x) * (T)
4. T = (x) * (T)
5. T = T + 6
6. T = (x) * T
7. T = T + 5

Thus, we need 7 operations if we are to use only one temporary variable.

如果您在以上文章中发现任何错误,请在下面发表评论。
这个问题的测验