Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 关于两个相关循环的复杂性的困惑? (i=1;i_Algorithm_Time Complexity_Big O - Fatal编程技术网

Algorithm 关于两个相关循环的复杂性的困惑? (i=1;i

Algorithm 关于两个相关循环的复杂性的困惑? (i=1;i,algorithm,time-complexity,big-o,Algorithm,Time Complexity,Big O,,您可以将这两个嵌套循环视为检查nxn矩阵上对角线上和对角线下的所有单元格 因此,您将始终执行接近N^2的1/2的操作数。因此,代码的操作总数将是N^2*常量。根据大O表示法的定义,这意味着您的代码运行时复杂性为O(N^2) 下面是一个简单的代码,可以帮助您理解我的解释 for(i=1; i < n; i++){ for(j=1; j <= i; j++){ statement1; } } #包括 #包括 使用std::vector; 使

,您可以将这两个嵌套循环视为检查nxn矩阵上对角线上和对角线下的所有单元格

因此,您将始终执行接近N^2的1/2的操作数。因此,代码的操作总数将是N^2*常量。根据大O表示法的定义,这意味着您的代码运行时复杂性为O(N^2)

下面是一个简单的代码,可以帮助您理解我的解释

for(i=1; i < n; i++){
   for(j=1; j <= i; j++){
         statement1;
   }       
}
#包括
#包括
使用std::vector;
使用std::cout;
使用std::endl;
//这些函数计算代码执行语句1的次数
整数计数(整数N){
int-total=0;
对于(int l=0;l对于(int r=0;r,您可以将这两个嵌套循环视为检查nxn矩阵上对角线上和对角线下的所有单元格

因此,您将始终执行接近N^2的1/2的操作数。因此,代码的操作总数将是N^2*常量。根据大O表示法的定义,这意味着您的代码运行时复杂性为O(N^2)

下面是一个简单的代码,可以帮助您理解我的解释

for(i=1; i < n; i++){
   for(j=1; j <= i; j++){
         statement1;
   }       
}
#包括
#包括
使用std::vector;
使用std::cout;
使用std::endl;
//这些函数计算代码执行语句1的次数
整数计数(整数N){
int-total=0;
对于(int l=0;l对于(int r=0;r而言,唯一值得计算的是执行
语句1
的频率

因此,请注意

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

// These function count the number of times that your code will execute statement1
int count(int N){
    int total = 0;
    for(int l = 0; l < N; ++l){
        for(int r = 0; r <= l; ++r){
            total++;
        }
    }
    return total;
}

// this code will show the cells of the matrix that you are "executing"
int showMatrix(int N){
    vector<vector<bool> > mat(N, vector<bool>(N, false) );
    for(int l = 0; l < N; ++l){
        for(int r = 0; r <= l; ++r){
            mat[l][r] = true;
        }
    }

    for(int line = 0; line < N; ++line){
        for(int column = 0; column < N; ++column){
            cout << (mat[line][column] == true ? "1 " : "0 ");
        }
        cout << endl;
    }
}

int main(){
    showMatrix(10);
    cout << count(10) << endl;
    return 0;
}

唯一有趣的事情是执行
语句1
的频率

因此,请注意

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

// These function count the number of times that your code will execute statement1
int count(int N){
    int total = 0;
    for(int l = 0; l < N; ++l){
        for(int r = 0; r <= l; ++r){
            total++;
        }
    }
    return total;
}

// this code will show the cells of the matrix that you are "executing"
int showMatrix(int N){
    vector<vector<bool> > mat(N, vector<bool>(N, false) );
    for(int l = 0; l < N; ++l){
        for(int r = 0; r <= l; ++r){
            mat[l][r] = true;
        }
    }

    for(int line = 0; line < N; ++line){
        for(int column = 0; column < N; ++column){
            cout << (mat[line][column] == true ? "1 " : "0 ");
        }
        cout << endl;
    }
}

int main(){
    showMatrix(10);
    cout << count(10) << endl;
    return 0;
}

如果我们每次都有一分钱,这个问题就被问到了……outler循环=O(N),内环=O((N-1)/2)=O(N),Total=O(N*N),因为内环的行为类似于.0+1+2+3+4+…+N-1,这与数学级数公式N(N-1)是一样的/2.你已经回答了你自己的问题-这个总和已经考虑到了外循环的执行情况。然后将它乘以Nyes是没有意义的,这似乎是对我问题的回答。如果我们每次都有一分钱问这个确切的问题……外循环=O(N),内循环=O((N-1)/2)=O(N),总数=O(N*N)因为,内循环行为类似于.0+1+2+3+4+…+N-1,这与数学级数公式N(N-1)/2相同。你已经回答了你自己的问题-这个总和已经考虑了外循环的执行情况。然后将其乘以Nyes是没有意义的,这似乎是对我问题的回答。