2014-03-01 春季PAT 1073-1076解题报告
今天下午的PAT考试状态不理想,回来怒刷了一遍,解题报告如下:
1073. Scientific Notation (20)
基本模拟题,将一长串的科学计数转换为普通的数字表示方式。思路是是数组存储输入,然后找到指数位置,并根据指数的大小和正负对前面的小数进行针对性的处理。
#include <iostream> #include <cstring> #include <algorithm> using namespace std; class PAT{ public: enum { N = 10000, }; char num[N]; int finger,epos,length; void run(); }; void PAT::run() { scanf("%s", &num); length = strlen(num); epos = find(num, num + length, 'E') - num; sscanf(num + epos + 2, "%d", &finger); if (num[epos + 1] == '-') finger = -1 * finger; if(num[0]=='-')printf("%c", num[0]); int i, j; if (finger >= 0) { printf("%c", num[1]); for (i = 3; i < epos && finger > 0; i++,finger--) printf("%c", num[i]); if (i < epos) { printf("."); for (; i < epos; i++)printf("%c", num[i]); } else if (finger > 0) while (finger > 0){ printf("0"); finger--; } } else { printf("0."); finger++; while (finger < 0) {printf("0"); finger++;} printf("%c", num[1]); for (int i = 3; i < epos; i++)printf("%c", num[i]); } } int main() { //freopen("input.txt", "r", stdin); PAT *p = new PAT; p->run(); return 0; }
PAT 1074. Reversing Linked List (25)
这个题不难,关键是要细心,首先要看懂题意。题目说的逆转排序是指每隔K个节点就将这k个节点的顺序颠倒过来,而如果最后不足K个节点的话,是不进行颠倒的,我下午考试的时候把这个意思弄错了,以为最后的节点也要进行颠倒,结果当然不对。这个题可以用map来做,不过据我同学说,用map查询会超时,我没有这样试过,不知道究竟如何,我使用的是hash的办法,直接在数组里面存储,速度会快点。
另外,这个题跟前面1052题的链表排序一样,有一个陷阱,就是不是所有给出的节点都属于此链表,需要判断,最后一个测试点是这个陷阱,不过这个点只有1分,姥姥出题的时候还是很人道。不过很多人这个点都没过。
#include <iostream> using namespace std; class PAT { public: enum{N=100000}; struct Node { int addr, data, next; }; Node node[N]; int hash[N];//addr->index int addr[N];//index->addr int n,k,head; void run(); }; void PAT::run() { scanf("%d%d%d", &head, &n, &k); for (int i = 0; i < n; i++) { scanf("%d%d%d", &node[i].addr, &node[i].data, &node[i].next); hash[node[i].addr] = i; } int cur = head,c=0; while (cur != -1) { addr[c] = node[hash[cur]].addr; cur = node[hash[cur]].next; c++; //节点计数 } n = c; //排除不在链表中的节点 for (int i = 0; i < n; i = i + k) { int r = (i + k<= n) ? i + k - 1 : n - 1; if (i + k <= n) { for (int j = r; j > i; j--) { node[hash[addr[j]]].next = node[hash[addr[j - 1]]].addr; } if (r == n - 1) node[hash[addr[i]]].next = -1; else node[hash[addr[i]]].next = (i + 2 * k <= n) ? node[hash[addr[i + 2*k -1]]].addr : node[hash[addr[i+k]]].addr; if (r!=n-1) for (int j = r; j >= i; j--) printf("%05d %d %05d\n", node[hash[addr[j]]].addr, node[hash[addr[j]]].data, node[hash[addr[j]]].next); else { for (int j = r; j > i; j--) printf("%05d %d %05d\n", node[hash[addr[j]]].addr, node[hash[addr[j]]].data, node[hash[addr[j]]].next); printf("%05d %d -1\n", node[hash[addr[i]]].addr, node[hash[addr[i]]].data); } } else { for (int j = i; j < n-1; j++) printf("%05d %d %05d\n", node[hash[addr[j]]].addr, node[hash[addr[j]]].data, node[hash[addr[j]]].next); printf("%05d %d -1\n", node[hash[addr[n-1]]].addr, node[hash[addr[n-1]]].data); } } } int main() { //freopen("input.txt", "r", stdin); PAT *p = new PAT; p->run(); return 0; }
这个题是一个常规的排序题,排序的时候仔细点。有一个要注意的地方是,如果提交了没有编译通过,submit里面显示的分数是-1,但是最终输出要输出为0。
#include <iostream> #include <algorithm> using namespace std; struct User { int id, total, score[6], rank, solved; bool onlist; }; class PAT { public: enum{N=10000}; User user[N]; int fullscore[N]; int n, k, m; void run(); void printscore(const User &u); }; bool cmp(const User &u1, const User &u2) { if (u1.total != u2.total) return u1.total > u2.total; else if (u1.solved != u2.solved) return u1.solved > u2.solved; else return u1.id < u2.id; } void PAT::printscore(const User &u) { for (int i = 1; i <= k; i++) { if (u.score[i] == -2) printf(" -"); else if (u.score[i] == -1) printf(" 0"); else printf(" %d", u.score[i]); } } void PAT::run() { scanf("%d%d%d", &n, &k, &m); for (int i = 0; i<n; i++) { user[i].id = i+1; user[i].total = 0; user[i].solved = 0; user[i].onlist = false; for (int j = 1; j <= k; j++) user[i].score[j] = -2; } for (int i = 1; i <= k; i++) scanf("%d", &fullscore[i]); int tid, tpid, tscore; while (m-- > 0) { scanf("%d%d%d", &tid, &tpid, &tscore); if (user[tid-1].score[tpid] < tscore) user[tid-1].score[tpid] = tscore; } for (int i = 0; i < n; i++) { for (int j = 1; j <= k; j++) { if (user[i].score[j] >= 0) { user[i].total += user[i].score[j]; user[i].onlist = true; } if (user[i].score[j] == fullscore[j]) user[i].solved++; } } sort(user, user + n, cmp); user[0].rank = 1; for (int i = 1; i < n; i++) { if (user[i].total == user[i - 1].total) user[i].rank = user[i - 1].rank; else user[i].rank = i+1; } for (int i = 0; i < n; i++) { if (user[i].onlist) { printf("%d %05d %d", user[i].rank, user[i].id, user[i].total); printscore(user[i]); printf("\n"); } else break; } } int main() { //freopen("input.txt", "r", stdin); PAT *p = new PAT; p->run(); return 0; }
这个题直接用BFS搜索遍历即可。
#include <iostream> #include <vector> using namespace std; class PAT { public: enum{N=1001}; vector<int> fans[N]; vector<int> curfans; int vis[N]; int n, level, k; void run(); void bfs(int); int maxforward; }; void PAT::run() { scanf("%d%d", &n, &level); int tnum,tfol; for (int i = 1; i <= n; i++) { scanf("%d", &tnum); while (tnum-- > 0) { scanf("%d", &tfol); fans[tfol].push_back(i); } } scanf("%d", &k); int qnum; while (k-- > 0) { scanf("%d", &qnum); fill(vis, vis + N, 0); curfans.clear(); maxforward = 0; vis[qnum] = 1; int fansize = fans[qnum].size(); for (int i = 0; i < fansize; i++) { curfans.push_back(fans[qnum][i]); vis[fans[qnum][i]] = 1; } bfs(level); printf("%d\n", maxforward); } } void PAT::bfs(int l) { if (l == 0) return; if (curfans.empty()) return; int fansize = curfans.size(); maxforward += fansize; vector<int> tmp; for (int i = 0; i < fansize; i++) { int fansize2 = fans[curfans[i]].size(); vector<int> &cur = fans[curfans[i]]; for (int j = 0; j < fansize2; j++) { if (vis[cur[j]] == 0) { tmp.push_back(cur[j]); vis[cur[j]] = 1; } } } curfans.clear(); curfans.resize(tmp.size()); copy(tmp.begin(), tmp.end(), curfans.begin()); bfs(l-1); } int main() { //freopen("input.txt", "r", stdin); PAT *p = new PAT; p->run(); return 0; }
本文出处:http://www.cnblogs.com/richard-g/
本作品采用知识共享署名 4.0 国际许可协议进行许可,欢迎转载,但是必须保留本文的署名和链接。