• 2022-05-28
    中国大学MOOC: 给定以下算法:bool isDuplicate(float* iArrayA, int n){ for (int i = 0; i < n; i++){ for (int j = i + 1; j < n; j++) if (iArrayA[i] == iArrayA[j]) return true; } return false;}其时间复杂度是O( )。[/i]
  • O(n^2)

    内容

    • 0

      针对如下三个实现矩阵求和的不同函数:int a&#91;N&#93;&#91;N&#93;;int sumA( int a&#91;N&#93;&#91;N&#93; ){ int i, j; int sum = 0; for ( i = 0; i &lt; N; i++ ) for ( j = 0; j &lt; N; j++ ) sum += a[i]&#91;j&#93;; return sum;}int sumB( int a&#91;N&#93;&#91;N&#93; ){ int i, j; int sum = 0; for ( j = 0; j &lt; N; j++ ) for ( i = 0; i &lt; N; i++ ) sum += a[i]&#91;j&#93;; return sum;}int sumC( int a&#91;N&#93;&#91;N&#93; ){ int i, j; int sum = 0; for ( j = 0; j &lt; N; j+=2 ) for ( i = 0; i &lt; N; i+=2 ) sum += ( a[i]&#91;j&#93; + a&#91;i+1&#93;&#91;j&#93; + a[i]&#91;j+1&#93; + a&#91;i+1&#93;&#91;j+1&#93; ); return sum;}当N足够大的时候,三个函数的运行时间t1、t2、t3符合下列哪种情况?()[/i][/i][/i][/i] A: t1 &gt; t2 &gt; t3 B: t3 &gt; t1 &gt; t2 C: t2 &gt; t3 &gt; t1 D: t3 &gt; t2 &gt; t1

    • 1

      以下程序的运行结果为:int fun(int n){ if(n==1) return 1; else return fun(n-1)+1;} int main(){ int i,j=0; for(i=1;i<3;i++) j += fun(i); printf("%d\n",j); return 0;}

    • 2

      【单选题】以下算法的时间复杂度() void matrimult(int a[M][N],int b[N][L],int c[M][L]) // { int i,j,k; for(i=0;i<M;i++) for(j=0;j<L;j++) c[i][j]=0; for(i=0;i<M;i++) for(j=0;j<L;j++) for(k=0;k<N;k++) c[i][j]+=a[i][k]*b[k][j]; } A. O(n*l) B. O(m*l) C. O(m*n) D. O(m*n*l)

    • 3

      写出以下程序运行结果。 #include<iomanip.h> const int N=3; int main() { int a&#91;N&#93;&#91;N&#93;={{7,-5,3},{2,8,-6},{1,-4,-2}}; int b&#91;N&#93;&#91;N&#93;={{3,6,-9},{2,-8,3},{5,-2,-7}}; int i,j,c&#91;N&#93;&#91;N&#93;; for(j=0;j<N;i++) //计算矩阵C for(j=0;j<N;j++) c[i]&#91;j&#93;=a[i]&#91;j&#93;+b[i]&#91;j&#93;; for(j=0;j<N;i++) //输出矩阵C { for(j=0;j<N;j++) cout<<setw(5)<<c[i]&#91;j&#93;; cout<<endl; } }[/i][/i][/i][/i]

    • 4

      分析以下算法的时间复杂度。void fun( int n)i{    int s = 0,i,i,k;    for (i - 0; i<= n; i++ )        for (j= 0;j<=i;j++)            for (k = 0;k<j;k++)                s tt ;}