AcWing 1224. 交换瓶子-最容易理解 可以AC
原题链接
中等
作者:
李_74
,
2021-02-05 23:05:24
,
所有人可见
,
阅读 422
C++ 代码
#include<iostream>
using namespace std;
const int N = 10000;
int a[N];
int main()
{
int n;
int cnt = 0;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
if (a[j] == i && j == i)
continue;
else if (j != i)
{
if (a[j] == i)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
cnt++;
}
}
}
}
/*for (int i = 1; i <= n; i++)
{
cout << a[i] << " ";
}*/
//cout << endl;
cout << cnt << endl;
return 0;
}