Sort with Swap(0,*)(25)
来源:互联网 发布:整进度计划软件 编辑:程序博客网 时间:2024/06/03 01:59
題目与华为的面试题类似,只能与0元素交换。
考虑情况:
1、0交换到了位置0,但仍有元素未被交换,需要多换2次。
2、初始位置为0,比不初始为0的多交换两次
3、计算环数与不在位个数。
代码:
#include "iostream"#include "vector"#include "string"#include "cstring"using namespace std;int main(){int N, group = 0, swap = 0;cin >> N;int num[100000];bool visit[100000];for (int i = 0; i < N; i++)cin >> num[i];memset(visit, false, sizeof(visit));for (int i = 0; i < N; i++){if (visit[i] || num[i] == i)continue;group++;int j = i;while (visit[j] == false){swap++;visit[j] = true;j = num[j];}}if (num[0])cout << swap + group - 2;else cout << swap + group;return 0;}
0 0
- Sort with Swap(0,*) (25)
- Sort with Swap(0,*)(25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- pat 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- PAT 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 1067. Sort with Swap(0,*) (25)
- 【PAT】1067. Sort with Swap(0,*) (25)
- 通过Angular-CLI创建目录
- (操作系统原理·第三章)读者-写者问题
- handler相关1
- 2017.3.17 信息课堂笔记
- 统一设备模型(一):开篇
- Sort with Swap(0,*)(25)
- c++知识体系
- jQuery中find和filter的区别
- myeclise10 svn: E210004: Number is larger than maximum 解决方案
- 总体性能——加载模块性能
- Qt 求两个多边形组合后的凸包,Qt里的排序方法
- Apache2.2.x 安装与配置详解
- sublime
- wireshark过滤抓包与过滤查看