我想計算圓形區域的大 O 符號。我想知道 Pi *r^2 的時間復雜度是多少?是 O(n) 還是 O(n^2)?還請檢查我的做法是否正確。
Algorithm
Step 1: Start // f(n)=O(1) ( Executes 1 time)
Step 2: Get an integer input from user for AREA and RADIUS//. f(n)=O(n)=O(1) ( Executes 1 time)
Step 3: Print the statement "Enter the Radius of Circle: //f(n)=O(n)=O(1) ( Executes 1 time)
Step 4: Calculate the area of the circle using the formula of
//f(n)=O(n)=?
(r - Radius) //f(n)=O(n)=O(1) ( Executes 1 time)
Step 5: Print the statement "Area of Circle:" //f(n)=O(n)=O(1) ( Executes 1 time)
Step 6: Print Area//f(n)=O(n)=O(1) ( Executes 1 time)
Step 7: Stop //f(n)=O(n)=O(1) ( Executes 1 time)
f(n)=O(n)=?
uj5u.com熱心網友回復:
這取決于您的計算模型。對于大多數實際目的而言O(1),因為運行時不依賴于您的輸入r。這隱含地假設您的輸入是有界的,例如適合 32 位整數。
更多的理論你可以想任意大r。大NO 符號中的通常是數字的長度(位數)r而不是數字的大小。在這里,您不能假設您可以將任意兩個數字相乘,O(1)但這取決于數字的長度。
在這種情況下,您將獲得O(N^2)一個簡單的乘法演算法,可以O(N log N)使用更高級的技術對其進行改進。請參閱https://en.wikipedia.org/wiki/Multiplication_algorithm#Computational_complexity_of_multiplication。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/529168.html
標籤:C 算法计算机科学
上一篇:A*搜索演算法示例
下一篇:列印*對應的迭代次數
