PAT总结(A1042,A1046,A1065)

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;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,505评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,150评论 0 23
  • 简介: 三姐妹安雨萱,安雨潇,杨晓,出生在小城镇,却立志要到大城市打拼,希望能拥有属于自己的一片天地,在爱情,工作...
    琉璃花楹阅读 626评论 6 14
  • 花散随风如断魂,今年花是去年尘。明年花色又还新。 且看花开花落里,何年不少赏花人。他年更化此花身。
    浩宇如烟阅读 315评论 0 2
  • 洗衣服用完了一整块肥皂,记笔记用掉了两支碳素笔和一整本笔记本,餐票也用完了,所以呢,上完明天的课,就该收拾收...
    Diamond二狗阅读 475评论 0 0