Making a square() function without x*x in C++

后端 未结 7 2119
攒了一身酷
攒了一身酷 2020-12-31 14:44

I am self-studying C++ and the book \"Programming-Principles and Practices Using C++\" by Bjarne Stroustrup. One of the \"Try This\" asks this:

Implement square() wi

7条回答
  •  失恋的感觉
    2020-12-31 15:26

    In term of the running time complexity,your implementation is clear and simply enough,its running time is T(n)=Θ(n) for input n elements.Of course you also can use Divide-and-Conquer method,assuming split n elements to n/2:n/2,and finally recursive compute it then sum up two parts,that running time will be like T(n)=2T(n/2)+Θ(n)=Θ(nlgn),we can find its running time complexity become worse than your implementation.

提交回复
热议问题