pat advanced level 02-20
Posted wifepi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pat advanced level 02-20相关的知识,希望对你有一定的参考价值。
1020 Tree Traversals (25分) ps:tmp定义在函数里过了,定在函数外面错了,人给整傻了
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10 ; int post[maxn], in[maxn], level[maxn] ; int n ; void DFS(int root, int st, int ed, int index) { if(st > ed) return ; level[index] = post[root] ; int tmp = st ; while(st < ed && in[tmp] != post[root]) ++ tmp ; DFS(root - 1 - ed + tmp, st, tmp - 1, index*2+1) ; DFS(root - 1, tmp + 1, ed, index*2+2) ; } int main(int argc, char const *argv[]) { scanf("%d",&n) ; for(int i = 0 ; i < n ; ++ i) scanf("%d",&post[i]) ; for(int i = 0 ; i < n ; ++ i) scanf("%d",&in[i]) ; memset(level,-1,sizeof level) ; DFS(n-1,0,n-1,0) ; for(int i = 0, cnt = 1 ; i < maxn ; ++ i) if(level[i] != -1) {printf("%d%c",level[i]," "[cnt == n]) ; ++ cnt ; if(cnt > n) break ;} return 0; }
1021 Deepest Root (25分) 找直径
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 10 ; int n, MAX = -1, ans = 0 ; int fa[maxn], deep[maxn] ; vector<int> v[maxn] ; int Find(int x) {return fa[x] == x ? x : fa[x] = Find(fa[x]) ;} void merge(int x, int y) {int fx = Find(x), fy = Find(y) ; if(fx != fy) fa[fx] = fy ;} void BFS(int x) { queue<int> q ; MAX = -1 ; while(!q.empty()) q.pop() ; for(int i = 1 ; i <= n ; ++ i) deep[i] = -1 ; deep[x] = 1 ; q.push(x) ; while(!q.empty()) { int tmp = q.front() ; q.pop() ; for(int i = 0 ; i < v[tmp].size() ; ++ i) if(deep[v[tmp][i]] == -1) deep[v[tmp][i]] = deep[tmp] + 1, q.push(v[tmp][i]), MAX = max(MAX, deep[tmp]+1) ; } } int main(int argc, char const *argv[]) { set<int> s ; s.clear() ; scanf("%d",&n) ; for(int i = 1 ; i <= n ; ++ i) fa[i] = i, v[i].clear() ; ans = 0 ; for(int i = 1, x, y ; i < n ; ++ i) scanf("%d %d",&x,&y), merge(x,y), v[x].push_back(y), v[y].push_back(x) ; for(int i = 1 ; i <= n ; ++ i) if(fa[i] == i) ++ ans ; if(ans > 1) {printf("Error: %d components ",ans) ; return 0 ;} BFS(1) ; for(int i = 1 ; i <= n ; ++ i) if(deep[i] == MAX) s.insert(i) ; BFS(*s.begin()) ; for(int i = 1 ; i <= n ; ++ i) if(deep[i] == MAX) s.insert(i) ; set<int> :: iterator iter = s.begin() ; for( ; iter != s.end() ; ++ iter) printf("%d ",*iter) ; return 0; }
1023 Have Fun with Numbers (20分) 字符串处理
#include <bits/stdc++.h> using namespace std; char arr[25], brr[25] ; int arr1[25], brr1[25], cnt1[15], cnt2[15] ; int main(int argc, char const *argv[]) { scanf("%s",arr) ; int n = strlen(arr) ; for(int i = 0 ; i <= 9 ; ++ i) cnt1[i] = cnt2[i] = 0 ; for(int i = 0 ; i < n ; ++ i) arr1[n - i - 1] = arr[i] - ‘0‘, cnt1[arr[i]-‘0‘] ++ ; int jin = 0 ; for(int i = 0 ; i < n ; ++ i) { brr1[i] = (arr1[i] * 2 + jin) % 10 ; if(arr1[i] * 2 + jin > 9) jin = 1 ; else jin = 0 ; } if(jin) brr1[n ++] = 1 ; for(int i = 0 ; i < n ; ++ i) cnt2[brr1[i]] ++ ; for(int i = 0 ; i < n ; ++ i) brr[n - i - 1] = brr1[i] + ‘0‘ ; for(int i = 0 ; i <= 9 ; ++ i) if(cnt1[i] != cnt2[i]) {printf("No %s ",brr) ; return 0 ;} printf("Yes %s ",brr) ; return 0; }
1024 Palindromic Number (25分) 字符串处理
#include <bits/stdc++.h> using namespace std; const int maxn = 110 ; char arr[maxn], brr[maxn] ; int arr1[maxn], brr1[maxn], tmp[maxn] ; int n, k, cnt = 1, jin = 0 ; bool check() { for(int i = 0 ; i < n ; ++ i) if(arr[i] != arr[n - i - 1]) return false ; return true ; } void solve() { for(int i = 0 ; i < n ; ++ i) arr1[n - i - 1] = arr[i] - ‘0‘, brr1[i] = arr[i] - ‘0‘ ; jin = 0 ; for(int i = 0 ; i < n ; ++ i) tmp[i] = (arr1[i] + brr1[i] + jin) % 10, jin = (arr1[i] + brr1[i] + jin) / 10 ; if(jin) tmp[n ++] = jin, jin = 0 ; for(int i = 0 ; i < n ; ++ i) arr[n - i - 1] = tmp[i] + ‘0‘ ; } int main(int argc, char const *argv[]) { scanf("%s %d",arr,&k) ; n = strlen(arr) ; cnt = 0 ; for(int i = 0; i < n ; ++ i) arr1[n - i - 1] = arr[i] - ‘0‘ ; while(cnt < k) { if(check()) {printf("%s %d ",arr,cnt) ; return 0 ;} else solve(), cnt ++ ; } printf("%s %d ",arr,k) ; return 0; }
1025 PAT Ranking (25分) 排序
#include <bits/stdc++.h> using namespace std; struct Student { char name[20] ; int score, location_number, local_rank, final_rank ; bool operator < (const Student & x) const { if(score != x.score) return score > x.score ; return strcmp(name,x.name) < 0 ; } }st[110][310], student[30030] ; int n, k, cnt = 0 ; int main(int argc, char const *argv[]) { scanf("%d",&n) ; cnt = 0 ; for(int j = 1 ; j <= n ; ++ j) { scanf("%d",&k) ; for(int i = 0 ; i < k ; ++ i) scanf("%s %d",st[j][i].name,&st[j][i].score) ; sort(st[j],st[j] + k) ; int tmp = 0 ; for(int i = 0 ; i < k ; ++ i) { tmp = i ; while(tmp > 0 && st[j][tmp].score == st[j][tmp - 1].score) tmp -- ; st[j][i].local_rank = tmp + 1 ; } for(int i = 0 ; i < k ; ++ i) strcpy(student[cnt].name, st[j][i].name), student[cnt].score = st[j][i].score, student[cnt].local_rank = st[j][i].local_rank, student[cnt ++].location_number = j ; } sort(student, student + cnt) ; for(int i = 0, tmp ; i < cnt ; ++ i) { tmp = i ; while(tmp > 0 && student[tmp].score == student[tmp - 1].score) tmp -- ; student[i].final_rank = tmp + 1 ; } printf("%d ",cnt) ; for(int i = 0 ; i < cnt ; ++ i) printf("%s %d %d %d ",student[i].name,student[i].final_rank,student[i].location_number,student[i].local_rank) ; return 0; }
以上是关于pat advanced level 02-20的主要内容,如果未能解决你的问题,请参考以下文章