| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:说下一种解法 329msIn Reply To:说下一种解法 329ms Posted by:785815369 at 2010-07-30 11:51:59 > 遍历位置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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator