Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

说下一种解法 329ms

Posted by 785815369 at 2010-07-30 11:51:59 on Problem 1674 and last updated at 2010-07-30 11:52:54
遍历位置1~n 当位置i的值a[i]!=i时说明不对 找到值为i的位置j 即a[j]==i 交换a[i]与
a[j] 同时总交换数sum++


核心代码如下

int swaps(int n)
{
	int sum=0;
	for(int i=1;i<n;i++)
	{
		if(a[i]!=i)
		{
			sum++;
			int j;
			for(j=i+1;j<=n;j++)
				if(a[j]==i)break;
			swap(a[i],a[j]);
		}
	}
	return sum;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator