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.
數(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.
數(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.
紙牌編號與花色的對應關系
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", ×);
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;
}