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}{要點}

數(shù)據(jù)預處理,避免超時
如果不經(jīng)預處理,可能會超時。在極端情況下,每次計算距離都要遍歷整個數(shù)組,需要10^5次操作,而共要計算10^4次,總結10^9次操作
使用 dis[i] 數(shù)組存放從 1 號頂點順時針到 i+1 號頂點的距離,因為存儲 1 號頂點到 1 號頂點的距離無意義

    //數(shù)據(jù)預處理
    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() {
    //數(shù)據(jù)預處理
    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}{要點}

數(shù)據(jù)溢出時的判別
當 A>0, B>0 , A+B<0 時發(fā)生正溢出,輸出true;
當 A<0, B<0, A+B>=0 時發(fā)生負溢出,輸出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;
}

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,109評論 0 23
  • 簡介: 三姐妹安雨萱,安雨瀟,楊曉,出生在小城鎮(zhèn),卻立志要到大城市打拼,希望能擁有屬于自己的一片天地,在愛情,工作...
    琉璃花楹閱讀 650評論 6 14
  • 花散隨風如斷魂,今年花是去年塵。明年花色又還新。 且看花開花落里,何年不少賞花人。他年更化此花身。
    浩宇如煙閱讀 333評論 0 2
  • 洗衣服用完了一整塊肥皂,記筆記用掉了兩支碳素筆和一整本筆記本,餐票也用完了,所以呢,上完明天的課,就該收拾收...
    Diamond二狗閱讀 537評論 0 0

友情鏈接更多精彩內(nèi)容