计算矩阵转置函数的步总数公式

#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++;
}

原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732309.html