收藏 分享(赏)

PAT历年真题参考代码.pdf

上传人:HR专家 文档编号:6298270 上传时间:2019-04-05 格式:PDF 页数:73 大小:1.39MB
下载 相关 举报
PAT历年真题参考代码.pdf_第1页
第1页 / 共73页
PAT历年真题参考代码.pdf_第2页
第2页 / 共73页
PAT历年真题参考代码.pdf_第3页
第3页 / 共73页
PAT历年真题参考代码.pdf_第4页
第4页 / 共73页
PAT历年真题参考代码.pdf_第5页
第5页 / 共73页
点击查看更多>>
资源描述

1、 PAT 真题 解析 参考代码 PAT20110828 A. World Cup Betting (20) 1. C 语言 版本 #include int main() double p3, t; int i, j, k; for (i = 0; i pi ) pi = t; k = j; switch (k) case 0: printf(“W “); break; case 1: printf(“T “); break; case 2: printf(“L “); break; printf(“%.2lfn“, (p0 * p1 * p2 * 0.65 - 1.0) * 2.0); ret

2、urn 0; 2. Lua 语言版本 str = tag = WTL result = 1.0 for line in io.lines() do index = 0 maxv = 0 for v in string.gmatch(line, %s+) do index = index + 1 v = tonumber(v) if v maxv then maxv = v maxi = index end end str = str string.sub(tag, maxi, maxi) result = result * maxv end print(string.format(str %.

3、2f, result * 1.3 - 2) B. The Best Rank (25) 1. C 语言 版本 #include #include #include struct country int A, C, M, E; int rank, type,tmprank, index, id; record2000, tmpr; int mapindex1000000; /定义 结构体,分别记录 3个 分值以及平均分 、 排序以及索引。 int comparA(const void *a, const void *b) return (const struct country*)a)-A A

4、? 1 : 0; int comparC(const void *a, const void *b) return (const struct country*)a)-C C ? 1 : 0; int comparM(const void *a, const void *b) return (const struct country*)a)-M M ? 1 : 0; int comparE(const void *a, const void *b) return (const struct country*)a)-E E ? 1 : 0; int comparInd(const void *a

5、, const void *b) return (const struct country*)a)-index (const struct country*)b)-index ? 1 : 0; void mysort(int n, int (*cmp)() int i, j; for ( i=1; i0) j- ) recordj.A = recordj-1.A; recordj.C = recordj-1.C; recordj.M = recordj-1.M; recordj.E = recordj-1.E; recordj.rank = recordj-1.rank; recordj.ty

6、pe = recordj-1.type; recordj.index = recordj-1.index; recordj.id = recordj-1.id; recordj.A = tmpr.A; recordj.C = tmpr.C; recordj.M = tmpr.M; recordj.E = tmpr.E; recordj.rank = tmpr.rank; recordj.type = tmpr.type; recordj.index = tmpr.index; recordj.id = tmpr.id; int main() int n, m, p, i; for (i=0;

7、i #include #include using namespace std; const int N = 2000; struct Student int id, best; char tag; int a4, rank4; void read() cin id; for (int i = 1; i ai; a0 = (a1 + a2 + a3) / 3.0; void sort() int k = 0; char str10 = “ACME“; for (int i = 1; i ); for (int i = 0; i n m; vector a(n); map mp; for (in

8、t i = 0; i #include #include #include using namespace std; #define N 1001 vectorGN; int visN, Time, delPoint;/这里利用时间戳优化,避免每次 vis 数组的初始化 void dfs(int u) visu = Time; for(int i = 0; i q; q.push(u); while(!q.empty() u = q.front(); q.pop(); for(int i = 0; i #define MAXN 20 #define MAXM 10 #define MAXK 2

9、000 struct queue int t, index; QMAXNMAXM; int cntMAXN, frontMAXN, rearMAXN, TMAXK, time, N, M, avalMAXN; void InitializeQ() int i; for (i=0; i #include #define MAXD 20 int NMAXD+1; int doubleN( int l ) int i, carry = 0; N0 = N0 9) carry = 1; N0 -= 10; for (i=1; i 9) carry = 1; Ni -= 10; else carry =

10、0; if (carry) Nl = 1; l+; return l; int main() char sMAXD+1; int i, l, ll, cnt10; for (i=0; i=0; i-) Ni = sl-i-1-0; cntNi +; ll = doubleN(l); if (ll l) printf(“Non“); else for (i=0; i=0; i-) printf(“%d“, Ni); printf(“n“); return 0; 2. Python 语言版本 def check(length, num, mp): # 判断 num中的数字个数与 mp 表是否一一对

11、应 if length != len(num): return False for i in xrange(length): mpnumi = mp.get(numi, 0) - 1 if mpnumi #include #define MAXD 100 char S1MAXD, SMAXD; int IsSym( int L ) int i, j; int flag = 1; i = 0; j = L-1; while ( i9) carry = 1; n -= 10; else carry = 0; S1i = n+0; if (carry) S1L = 1; int main() int

12、 l, k, count = 0; scanf(“%s %d“, S1, l = strlen(S1); while ( !IsSym(l) ) InverseS1(l); Add(l); l = strlen(S1); count+; if (count = k) break; InverseS1(l); printf(“%sn“, S); printf(“%dn“, count); return 0; 2. Python 语言版本 def isPalindromic(s): l = 0 r = len(s) - 1 while l /#include #include #include #

13、define MAXN 100 #define MAXK 300 #define MAXS 30000 /* MAXN*MAXK */ struct student int score, loc, lrank, frank; char id14; StuMAXS; int comparS(const void *a, const void *b) return (const struct student*)a)-score score ? 1 : 0; int comparId(const void *a, const void *b) return strcmp( (const struct

14、 student*)a)-id, (const struct student*)b)-id ) 0 ? 1 : 0; void copystu( struct student S1, struct student *S2 ) int i; for (i=0; i0) j- ) copystu( Sj-1, copystu( tmp, void LRank(struct student S, int n) int i, sn; S0.lrank = 1; sn = 0; for (i=1; i=0) StuP-; else Stucur = SSP; SP-; cur-; while ( SP=

15、0 ) StuSP = SSP; SP-; return (SN+n); int main() int N, K, lk; struct student SMAXK; int i, j; K = 0; scanf(“%d“, for (i=0; i #include #include #include using namespace std; struct Student Student(string registrationNumber = “, int score = 0, int locationNumber = 0):registrationNumber(registrationNum

16、ber), score(score), locationNumber(locationNumber) finalRank = locationRank = 0; bool operator that.score; return registrationNumber students; int m; cin m; for (int i = 1; i locals; int n; cin n; for (int j = 0; j registrationNumber score; locals.push_back(Student(registrationNumber, score, i); sor

17、t(locals.begin(), locals.end(); for (int i = 0; i :const_iterator it = students.cbegin(); it != students.cend(); +it) printf(“%s %d %d %dn“, it-registrationNumber.c_str(), it-finalRank, it-locationNumber, it-locationRank); return 0; D. Table Tennis (30) #include #include #define MaxProc 120 #define

18、MaxWindow 100 #define MaxPlayer 10000 / 选手结构体 struct People int T; int P; int VIP; ; / 队列结构体。 VIP 选手设计了另一个队列来存储位置信息。 struct QueueRecord int front; int rear; int size; int VIPfront; int VIPrear; int VIPsize; struct People *Customer; int *VIPCustomer; ; typedef struct QueueRecord *Queue; Queue CreateQ

19、ueue( int MaxElements ); void AddQ( Queue Q, struct People X ); void AddVIP( Queue Q, int Position ); struct People DeleteQ( Queue Q ); struct People DeleteVIP( Queue Q ); int IsEmpty( Queue Q ); / 按入队时间顺序对所有候选选手进行排序 void mysort( Queue Q, int n) int i, j, tt, pp, tvip; for ( i=1; iCustomeri.T; pp =

20、Q-Customeri.P; tvip = Q-Customeri.VIP; for ( j=i; (j0) j- ) Q-Customerj.T = Q-Customerj-1.T; Q-Customerj.P = Q-Customerj-1.P; Q-Customerj.VIP = Q-Customerj-1.VIP; Q-Customerj.T = tt; Q-Customerj.P = pp; Q-Customerj.VIP = tvip; / 读入数据并创建候选队列 Queue CreateQueue( int MaxElements ) Queue Q; struct People

21、 X; int i, hh, mm, ss; Q = malloc( sizeof( struct QueueRecord ) ); Q-Customer = malloc( sizeof( struct People ) * MaxElements ); Q-VIPCustomer = malloc( sizeof(int) * MaxElements ); Q-size = 0; Q-front = 0; Q-rear = -1; Q-VIPsize = 0; Q-VIPfront = 0; Q-VIPrear = -1; for ( i=0; iMaxProc) X.P = MaxPro

22、c; X.P *= 60; AddQ( Q, X ); mysort( Q, Q-size ); for ( i=0; iCustomeri.VIP ) AddVIP( Q, i ); return Q; / 将新的普通候选人添加至队列 void AddQ( Queue Q, struct People X ) Q-rear+; Q-CustomerQ-rear.T = X.T; Q-CustomerQ-rear.P = X.P; Q-CustomerQ-rear.VIP = X.VIP; Q-size+; / 将新的 VIP候选人添加至队列 void AddVIP( Queue Q, int

23、 Position ) Q-VIPrear+; Q-VIPCustomerQ-VIPrear = Position; Q-VIPsize+; / 从当前队列中取出第一个选手, T 为 -1表示队列已空 struct People DeleteQ( Queue Q ) struct People X; while ( Q-CustomerQ-front.VIP = -1 ) Q-front+; Q-size-; if ( IsEmpty(Q) ) X.T = -1; return X; if ( Q-CustomerQ-front.VIP = 1 ) X = DeleteVIP(Q); else

24、 X.T = Q-CustomerQ-front.T; X.P = Q-CustomerQ-front.P; X.VIP = Q-CustomerQ-front.VIP; Q-front+; Q-size-; return X; / 从当前队列中取出第一个 VIP选手 struct People DeleteVIP( Queue Q ) struct People X; int Position; if ( Q-VIPsize ) Position = Q-VIPCustomerQ-VIPfront; Q-VIPfront+; Q-VIPsize-; Q-CustomerPosition.VI

25、P = -1; / 将选手的 VIP 标记置为 -1 表示更新为无效数据 X.T = Q-CustomerPosition.T; X.P = Q-CustomerPosition.P; X.VIP = Q-CustomerPosition.VIP; else X = DeleteQ(Q); return X; / 判断队列是否为空 int IsEmpty( Queue Q ) return ( Q-size = 0 ); / 判断当前时间是否有 VIP选手到达 int IsVipHere( Queue Q, int CurrentTime ) int Position; if ( Q-VIPs

26、ize ) Position = Q-VIPCustomerQ-VIPfront; if (CurrentTime = Q-CustomerPosition.T) return 1; return 0; / 从当前的所有 table 中找出所需剩余时间最少的 int FindNextWindow( int W, int K, int *WaitTime ) int WinAvail, MinW = MaxProc*60+1; int i; for ( i=0; i 75599) break; PrintTime(Next.T); printf(“ “); PrintTime(CurrentTi

27、me); printf(“ %dn“, (int)(double)CurrentTime-(double)Next.T)/60.0+0.5); WindowWinAvail = Next.P; CountWinAvail +; printf(“%d“, Count0); for ( i=1; i #define MAXN 100000 #define MAXkey 10000 struct Key int cnt; int no; KMAXkey; int main() int i, n, m; int ind = MAXN; for (i=0; i #define MAXL 1000 char SMAXL; int IsSym( int begin, int end) int i, j, mid; mid = begin + (end va- begin + 1) 1); j = end; for (i=begin; ii; j-) if ( (Sj=Si) break; printf(“%dn“, maxl); return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报