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

#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("\n");
	for(int i=0; i<MAX_SIZE; i++)
	    for(int j=0; j<MAX_SIZE; j++)
			printf("%d\n", a[i][j]);
	
	printf("\n%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++;
}

posted on 2017-12-30 11:37  MACHINE_001  阅读(244)  评论(0编辑  收藏  举报

导航