Big o 产生多项式的计数运算 为了生成多项式f(x),我正在努力精确计算此方法的运算: publicstaticintnumoccurrences(intn){ 整数计数=0; 对于(int i=0;i

Big o 产生多项式的计数运算 为了生成多项式f(x),我正在努力精确计算此方法的运算: publicstaticintnumoccurrences(intn){ 整数计数=0; 对于(int i=0;i,big-o,counting,polynomials,operations,Big O,Counting,Polynomials,Operations,我想分享我的答案,以防有人无意中发现这篇文章。 我今天从一位导师那里发现了一种更简单的生成多项式的方法,那就是计算每个操作的次数,但是我们不需要写出循环中的每件事都发生了n*n次,而是简单地计算单个步骤,并将这些单独的步骤(如I++)嵌套在n中 例如: int count = 0; //1 int i = 0; //+ 1 while(i < n) //+ n( i++; //+ 1 int j = 0; //+ 1 while(j &

我想分享我的答案,以防有人无意中发现这篇文章。 我今天从一位导师那里发现了一种更简单的生成多项式的方法,那就是计算每个操作的次数,但是我们不需要写出循环中的每件事都发生了n*n次,而是简单地计算单个步骤,并将这些单独的步骤(如I++)嵌套在n中

例如:

int count = 0;  //1 
int i = 0;      //+ 1 
while(i < n)    //+ n(
i++;            //+ 1
int j = 0;      //+ 1
while(j < n)    //+ n(
j++             //+ 1
if(i == j)      //+ 1
continue;       //+ 1
if(strings[i] == strings[j]) //+ 3
count++;        //+ 1
return count;   //+ 1
int count=0;//1
int i=0;//+1
而(i
把它们加起来,我们得到=1+1+n(1+1+n(1+1+1+3+1))+1

简化=6n^2+2n+3

    int count = 0;  //1 time
    int i = 0;      //1 time
    while(i < n)    //n times
    int j = 0;      //n times
    while(j<n)      //n*n times 
    if(i == j)      //n*n times
    continue;       //n times
    if(strings[i] == strings[j]) //n*n+2 times
    count++;        //n*n times  
    i++             //n*n times
    j++             //n*n times
    return count;   //1 time
   
int count = 0;  //1 
int i = 0;      //+ 1 
while(i < n)    //+ n(
i++;            //+ 1
int j = 0;      //+ 1
while(j < n)    //+ n(
j++             //+ 1
if(i == j)      //+ 1
continue;       //+ 1
if(strings[i] == strings[j]) //+ 3
count++;        //+ 1
return count;   //+ 1