PAT 甲级 A1067 (2019/02/20)

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100010;
int a[MAXN];        //存放各个数字当前所处的位置编号
int main(){
    int n, num, ans = 0;//表示总计交换次数
    scanf("%d", &n);
    int surplus = n - 1;//存放除0以外不在本位上的数的个数
    for(int i = 0; i < n; i++){
        scanf("%d", &num);
        a[num] = i;  //num所处的位置为i
        if(num == i && num != 0)//除了零,在本位上的数
            surplus--;
    }
    int k = 1;      //k存放除了0以外当前不在本位上的最小的数
    while(surplus > 0) {//只要有数还不在本位上
        //如果0在本位上,则寻找一个当前不在本位上的数与0交换
        if(a[0] == 0) {
            while(k < n) {
                if(a[k] != k) { //找到一个当前不在本位上的数k
                    swap(a[0], a[k]);       //k与0交换位置
                    ans++;      //交换次数加1
                    break;      //退出循环
                }
                k++;            //判断k+1是否在本位
            }
        }
        //只要0不在本位,就将0所在位置的数的当前所处位置与0交换
        while(a[0] != 0) {
            swap(a[0], a[a[0]]);    //0与a[0]交换
            ans++;          //交换次数加1
            surplus--;      //不在本位上的数的个数减1
        }
    }
    printf("%d", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/zjsaipplp/p/10425233.html

时间: 2024-11-01 11:05:39

PAT 甲级 A1067 (2019/02/20)的相关文章

PAT 甲级 A1010 (2019/02/20)

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; LL Map[256]; // 0 ~ 9, a ~ z 与 0 ~ 35 的对应 LL Y = 1; // 定义LL型的Y LL inf = (1LL << 63) - Y; // long long 的最大值 2^63 - 1,注意括号 void init

PAT 甲级 A1037 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; //段错误,数组开辟的太小 int a[MAXN], b[MAXN]; int main(){ int n1, n2; scanf("%d", &n1); for(int i = 0; i < n1; i++){ scanf("%d", &a[i]); }

PAT 甲级 A1044 (2019/02/20)

#include<cstdio> const int MAXN = 100010; int sum[MAXN]; int n, S, nearS = 100000010; int upper_bound(int L, int R, int x) { int left = L, right = R, mid; while(left < right) { mid = (left + right) / 2; if(sum[mid] > x) { right = mid; } else {

PAT 甲级 A1085 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int n, p, a[MAXN]; int binarySearch(int i, long long x){ if(a[n - 1] <= x) //如果最大的数比x小,则返回n return n; int left = i + 1; int right = n - 1; //在区间[i+1, n-1]内查找

PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone c

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT甲级——A1067 Sort with Swap(0, i)

Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the fo

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a