zoukankan      html  css  js  c++  java
  • 计算矩阵转置函数的步总数公式

    #include<stdio.h>
    
    int count=0;
    
    #define MAX_SIZE 2
    #define SWAP(x, y, t) ((t)=(x), (x)=(y),(y)=t)
    
    void transpose(int a[][MAX_SIZE]);
    
    int main(void)
    {
    	int a [MAX_SIZE][MAX_SIZE]={{1,2},
    	                            {3,4}};
    	for(int i=0; i<MAX_SIZE; i++)
    	    for(int j=0; j<MAX_SIZE; j++)
    			printf("%d", a[i][j]);
    	transpose(a);
    	printf("
    ");
    	for(int i=0; i<MAX_SIZE; i++)
    	    for(int j=0; j<MAX_SIZE; j++)
    			printf("%d
    ", a[i][j]);
    	
    	printf("
    %d", count);
    	return 0;
    }
    
    void transpose(int a[][MAX_SIZE])
    {
    	int i,j,temp;
    	for(i=0; i<MAX_SIZE; i++)   //se:1 频率:n+1 步数:n+1
    	{   
    		count++;
    		for(j=i+1; j<MAX_SIZE; j++) 
    			                  //se:1 频率:(n(n+1))/2 步数:(n²+n)/2
    		{
    			count+=2;
    //			SWAP(a[i][j], a[j][i], temp);
                                  //se:1 (n(n-1)/2) 步数:(n²-n)/2
                                  //公式:1+n+n²
    		}
    		count++;
    	}
    	count++;
    }
    

  • 相关阅读:
    5 静态链接和动态链接
    4 程序编译与链接
    3.死锁
    2.调度算法
    1 select,poll和epoll
    python语言特性
    python动态规划
    python语言编程算法
    链表
    认识黑客常用的入侵方法
  • 原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732309.html
Copyright © 2011-2022 走看看