美文网首页PAT试题
PAT总结(A1042,A1046,A1065)

PAT总结(A1042,A1046,A1065)

作者: 沂雩归 | 来源:发表于2019-08-02 16:11 被阅读0次

    PAT总结(A1042,A1046,A1065)


    A1046 Shortest Distance

    题目

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

    \color{red}{要点}

    数据预处理,避免超时
    如果不经预处理,可能会超时。在极端情况下,每次计算距离都要遍历整个数组,需要10^5次操作,而共要计算10^4次,总结10^9次操作
    使用 dis[i] 数组存放从 1 号顶点顺时针到 i+1 号顶点的距离,因为存储 1 号顶点到 1 号顶点的距离无意义

        //数据预处理
        int sum=0, n;
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i){
            scanf("%d", &A[i]);
            sum += A[i];
            dis[i] = sum;
        }
    
    参考代码
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int MAXN=100005;
    int dis[MAXN],A[MAXN];
    
    int main() {
        //数据预处理
        int sum=0, n;
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i){
            scanf("%d", &A[i]);
            sum += A[i];
            dis[i] = sum;
        }
        //开始处理
        int  linesNum, left, right;
        scanf("%d", &linesNum);
        for(int i = 0; i < linesNum; ++i){
            scanf("%d%d", &left, &right)//(left,right);
            if(left > right) swap(left, right)//保证right>left;
            int temp = dis[right-1] - dis[left-1];
            printf("%d\n",min(temp, sum-temp));
        }
        return 0;
    }
    

    1065 A+B and C (64bit)

    题目

    Given three integers A, B and C in [−263,263], you are supposed to tell whether A+B>C.

    \color{red}{要点}

    数据溢出时的判别
    当 A>0, B>0 , A+B<0 时发生正溢出,输出true;
    当 A<0, B<0, A+B>=0 时发生负溢出,输出false

            sum = a + b;
            if(a>0&&b>0&&sum<0) flag=true;//正溢出
            else if(a<0&&b<0&&sum>=0) flag=false;//负溢出
    
    参考代码
    #include <cstdio>
    int main() {
        int n;
        scanf("%d",&n);
        long long a, b, c;
        long long sum;
        bool flag;
        for(int i=0;i<n;++i){
            scanf("%lld%lld%lld",&a, &b, &c);
            sum = a + b;
            if(a>0&&b>0&&sum<0) flag=true;//正溢出
            else if(a<0&&b<0&&sum>=0) flag=false;//负溢出
            else if(sum>c) flag=true;
            else flag=false;
            if(flag)
                printf("Case #%d: true\n",i+1);
            else
                printf("Case #%d: false\n",i+1);
        }
        return 0;
    }
    

    A1042 Shuffling Machine

    题目

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.
    The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

    S1, S2, ..., S13,
    H1, H2, ..., H13,
    C1, C2, ..., C13,
    D1, D2, ..., D13,
    J1, J2
    where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

    \color{red}{要点}

    纸牌编号与花色的对应关系

        char mp[5] = {'S', 'H', 'C', 'D', 'J'};
        int counts = 0;
        for (int i = 1; i <= 54; ++i) {
            printf("%c%d", mp[(cards[i] - 1) / 13], (cards[i] - 1) % 13 + 1);
            counts++;
            if (counts != 54)
                putchar(' ');
        }
    
    参考代码
    #include <cstdio>
    
    int main() {
        int cards[55], tem[55], order[55];
        //initialize
        for (int i = 1; i <= 54; ++i) {
            cards[i] = i;
        }
    
        int times;
        scanf("%d", &times);
        for (int i = 1; i <= 54; ++i) {
            scanf("%d", &order[i]);
        }
        //shuffling
        while (times--) {
            for (int i = 1; i <= 54; ++i) {
                tem[order[i]] = cards[i];
            }
            for (int i = 1; i <= 54; ++i) {
                cards[i] = tem[i];
            }
        }
        //print
        char mp[5] = {'S', 'H', 'C', 'D', 'J'};
        int counts = 0;
        for (int i = 1; i <= 54; ++i) {
            printf("%c%d", mp[(cards[i] - 1) / 13], (cards[i] - 1) % 13 + 1);
            counts++;
            if (counts != 54)
                putchar(' ');
        }
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:PAT总结(A1042,A1046,A1065)

        本文链接:https://www.haomeiwen.com/subject/grdcdctx.html