函数的大O计算

时间:2016-09-25 16:47:50

标签: loops big-o

如何为函数计算O?

我需要知道这个功能的O. (以及每个循环)

def main():
    startup_message()
    name = input('Enter name: ')
    sales_amount = float(input('Enter sales amount: '))
    hours_worked = float(input('Enter hours worked: '))
    hourly_pay_amount = hours_worked * hourly_pay_rate
    commission_amount = sales_amount * commission_rate
    gross_pay = hourly_pay_rate + commission_rate
    withholding = gross_pay * withholding_rate
    net_pay = gross_pay - withholding
    display_results()#<-----'not defined' error for calculations

1 个答案:

答案 0 :(得分:1)

int find_c(int n)
  int i,j,c
  for(i=n*n; i > 1; i=i/4) //O(logn)
    for(j=n*n; j > n/2; j--) //O(n2)
      c++   //c = O(n2.logn)
  for(i=c; i > 0; i--) //O(n2.logn)
    if(random(0...99) > 0) //O(1)
      for(j=n; j > 0; j--) //O(n)
        c++ //O(1)  //c = O(n3logn)
    else
      for(j=400; j > 1; j--) //O(1)
        c++ //c = O(n2log2)
  return c

所以最终答案是O(n ^ 3.log n)