【PAT刷题】快速产出垃圾——2020-3-10

A1051

上边太长了,接上边解题思路。

解决本题的基本思路是:按照要求进行模拟,将1~n依次入栈,在入栈过程中如果入栈元素恰好等于出战序列当前等待出栈的元素,那么就让栈顶元素出栈,同时把出栈序列当前等待出栈的元素位置标记后移一位。此时只要栈顶元素仍然等于出栈序列当前待出栈的元素,则持续出栈。具体步骤如下:

步骤1:初始化栈,读入需要测试的出栈顺序。以bool变量flag表示出栈顺序是否合法。以int型变量current表示出栈序列当前等待出栈的元素位置标记,初值为1。

步骤2:由于入栈顺序为1~N,因此从1至N枚举i,对每一个i,先将i入栈。如果此时栈内元素数目大于m个(m为题设所允许的最大容量),则违反规则,置flag为false,退出循环;否则,反复判断当前current所指出栈序列的元素(即待出栈元素)是否等于栈顶元素,若是,则让元素出栈,并让 current自增以指向下一个待出栈元素。

步骤3:输出。

总结:入栈顺序已给出,按顺序入栈,入到所需元素,出栈,后移到不需要,然后继续往后入栈。所以就是一条线,入栈,每次入栈过程都反复检测栈顶是否是所需元素。

 

A1056

(1)自己尝试

 1 #include <cstdio>
 2 #include <queue>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct mouse {
 7     int weight;
 8     int order;
 9     int rank;
10     int id;
11 }mice[1010];
12 
13 queue<mouse> plys;
14 
15 bool cmp(mouse a,mouse b) {
16     if (a.order != b.order) return a.order < b.order;
17     else return a.weight < b.weight; //注意这里重量也是升序;
18 }
19 bool cmp2(mouse a, mouse b) {
20     return a.id < b.id;
21 }
22 int main() {
23     int np, ng;
24     scanf("%d%d", &np, &ng);
25     int np2=np;
26     for (int i = 0; i < np; i++) {
27         scanf("%d", &mice[i].weight);
28         mice[i].id = i;
29     }
30     for (int i = 0; i < np; i++) {
31         int tmp;
32         scanf("%d", &tmp);
33         mice[tmp].order = i / ng;
34     }
35     sort(mice, mice + np, cmp);
36     for (int i = 0; i < np; i++) {
37         plys.push(mice[i]);
38     }
39     queue<mouse> winers;
40     queue<mouse> losers;
41     while (plys.size() > 1) {
42         int count = 1;
43         int cg = (np - 1) / ng + 1;
44         int lastCt = np % ng;
45         while (winers.size() < cg) {
46             if (count == ng || (winers.size()==cg-1 && count==lastCt)) {
47                 winers.push(plys.front());
48                 plys.pop();
49                 count = count % ng + 1;
50             }
51             else {
52                 mouse tmpLo = plys.front();
53                 tmpLo.rank = cg + 1;
54                 losers.push(tmpLo);
55                 plys.pop();
56                 count++;
57             }
58         }
59         np = cg;
60         plys = winers;
61         for (int i = 0; i < np; i++) {
62             mice[i] = plys.front();
63             mice[i].order = mice[i].order / ng;
64             plys.pop();
65         }
66         sort(mice, mice + np, cmp);
67         for (int i = 0; i < np; i++) {
68             plys.push(mice[i]);
69         }
70         while (!winers.empty()) {
71             winers.pop();
72         }
73     }
74     mouse win = plys.front();
75     win.rank = 1;
76     losers.push(win);
77     for (int i = 0; i < np2; i++) {
78         mice[i] = losers.front();
79         losers.pop();
80     }
81     sort(mice, mice + np2, cmp2);
82     for (int i = 0; i < np2; i++) {
83         printf("%d", mice[i].rank);
84         if (i != np2-1) printf(" ");
85     }
86 }

彻底混乱了,题目没读懂,其实意思时6号老鼠排第一个,而不是一号老鼠排第六个。最后改一下读入就好了。不过写得过于麻烦了。

答案也很麻烦,算了算了,就这样好歹看了一眼就做对了。自信点自信点。

 

A1032

猴戏失败的原因是因为有无效结点的出现。实际上就考察一个链表的遍历。

(1)参考答案

 1 #include <cstdio>
 2 
 3 const int maxn = 100010;
 4 struct node
 5 {
 6     char chr;
 7     int next;
 8     bool flag;
 9 }wds[maxn];
10 
11 int main() {
12     for(int i=0;i<maxn;i++){
13         wds[i].flag =false;
14     }
15     int h1, h2, n;
16     scanf("%d%d%d", &h1, &h2, &n);
17     int ans = -1;
18     for (int i = 0; i < n; i++) {
19         int id, next;
20         char tmp;
21         scanf("%d %c %d", &id, &tmp, &next);
22         wds[id].next = next;
23         wds[id].chr = tmp;
24     }
25     for(int p=h1;p!=-1;p=wds[p].next){
26         wds[p].flag=true;
27     }
28     for(int p=h2;p!=-1;p=wds[p].next){
29         if(wds[p].flag){
30             ans = p;
31             break;
32         }
33     }
34     if(ans!=-1){
35     printf("%05d", ans);
36     }
37     else printf("-1");
38 }

分别遍历两个列表,思路还是很清晰的。

 

A1052

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int maxn = 100010;
 6 struct Node {
 7     int adress;
 8     int key;
 9     int next;
10     bool onLine;
11 }node[maxn];
12 
13 bool cmp(Node a,Node b) {
14     if (a.onLine != b.onLine) return a.onLine > b.onLine;
15     else return a.key < b.key;
16 }
17 
18 int main() {
19     for (int i = 0; i < maxn; i++) {
20         node[i].onLine = false;
21     }
22     int n, head;
23     scanf("%d%d", &n, &head);
24     for (int i = 0; i < n; i++) {
25         int ads, key, next;
26         scanf("%d%d%d", &ads, &key, &next);
27         node[ads].adress =ads;
28         node[ads].key = key;
29         node[ads].next = next;
30     }
31     int count = 0, p=head;
32     while(p!=-1) {
33         node[p].onLine = true;
34         count++;
35         p = node[p].next;
36     }
37     if(count == 0) printf("0 -1");
38     else{
39         sort(node, node + maxn, cmp);
40         printf("%d %05d\n", count, node[0].adress);
41         for (int i = 0; i < count; i++) {
42             if(i!=count-1) printf("%05d %d %05d\n", node[i].adress, node[i].key, node[i + 1].adress);
43             else printf("%05d %d -1", node[i].adress, node[i].key);
44         }
45     }
46     
47 }

最后那个坑是count,而不是输出n,然后就是单独考虑都没有的情况。

 

B1025/A1074 

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int maxn = 100010;
 6 
 7 struct  Node
 8 {
 9     int address, key,next;
10     int id;
11     bool flag;
12 }node[maxn];
13 
14 bool cmp(Node a, Node b) {
15     if (a.flag != b.flag) return a.flag > b.flag;
16     else return a.id < b.id;
17 }
18 
19 int main() {
20     for (int i = 0; i < maxn; i++) {
21         node[i].flag = false;
22     }
23     int head, n, m;
24     scanf("%d%d%d", &head, &n, &m);
25     for (int i = 0; i < n; i++) {
26         int ads, key, next;
27         scanf("%d%d%d", &ads, &key, &next);
28         node[ads].address = ads;
29         node[ads].key = key;
30         node[ads].next = next;
31     }
32     int count = 0;
33     for (int p = head; p != -1; p = node[p].next) {
34         node[p].flag = true;
35         node[p].id = count;
36         count++;
37     }
38     sort(node, node + maxn, cmp);
39     int i;
40     for (i = 0; i < count-m; i += m) {
41         reverse(node + i, node + i + m);
42     }
43     if (count - i == m) reverse(node + i, node + i + m);
44     for (int i = 0; i < count; i++) {
45         if (i != count - 1) printf("%05d %d %05d\n", node[i].address, node[i].key, node[i + 1].address);
46         else printf("%05d %d -1\n", node[i].address, node[i].key);
47     }
48 }

注意i的条件。

 

A1097

(1)自己尝试

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <vector>
 4 using namespace std;
 5 
 6 const int maxn = 100010;
 7 
 8 struct  Node
 9 {
10     int address, key,next;
11     int id;
12     int flag;
13 }node[maxn];
14 
15 bool has[10010];
16 
17 vector<Node> lose;
18 
19 bool cmp(Node a, Node b) {
20     if (a.flag != b.flag) return a.flag > b.flag;
21     else return a.id < b.id;
22 }
23 
24 int main() {
25     for (int i = 0; i < maxn; i++) {
26         node[i].flag = -1;
27     }
28     int head, n;
29     scanf("%d%d", &head, &n);
30     for (int i = 0; i < n; i++) {
31         int ads;
32         scanf("%d", &ads);
33         scanf("%d%d", &node[ads].key, &node[ads].next);
34         node[ads].address = ads;
35     }
36     int count1 = 0, count2 = 0;
37     for (int p = head; p != -1; p = node[p].next) {
38         int tmp = abs(node[p].key);
39         if (!has[tmp]) {
40             node[p].flag = 1;
41             node[p].id = count1;
42             count1++;
43             has[tmp] = true;
44         }
45         else
46         {
47             lose.push_back(node[p]);
48             count2++;
49         }
50     }
51     sort(node, node + maxn, cmp);
52     for (int i = 0; i < count1; i++) {
53         if(i!=count1-1) printf("%05d %d %05d\n", node[i].address, node[i].key, node[i + 1].address);
54         else printf("%05d %d -1\n", node[i].address, node[i].key);
55     }
56     for (int i = 0; i < count2; i++) {
57         if (i != count2 - 1) printf("%05d %d %05d\n", lose[i].address, lose[i].key, lose[i + 1].address);
58         else printf("%05d %d -1\n", lose[i].address, lose[i].key);
59     }
60 }

 

A1103

(1)自己写的拿了22分,看下答案

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cmath>
 4 using namespace std;
 5 
 6 const int maxn = 410;
 7 vector<int> ans, tmp; //存答案数组
 8 int sum2 = 0, sum1 = 0; //存答案总和
 9 int maxSum1 = 0; //存比较
10 int n, k, p;
11 
12 bool judge(vector<int> ans, vector<int> tmp) { //判断两组数据求和大小
13     int s1 = 0, s2 = 0;
14     if (ans.size() == 0) return true;
15     for (int i = 0; i < tmp.size(); i++) {
16         s1 += ans[i];
17         s2 += tmp[i];
18     }
19     if (s1 != s2) return s1 < s2;
20     else {
21         int t1 = 0, t2 = 0;
22         int i;
23         for (i = 0; i < tmp.size(); i++) {
24             t1 = ans[i];
25             t2 = tmp[i];
26             if (t1 = t2) break;
27         }
28         for (int j = i + 1; j < tmp.size(); j++) {
29             if (ans[j] != tmp[j]) return ans[j] < tmp[j];
30         }
31     }
32     return false;
33 }
34 
35 void DFS(int begin, int sum2, int count) {
36     if (count == k && sum2 == n) {
37         if (judge(ans, tmp)) {
38             maxSum1 = sum1;
39             ans = tmp;
40         }
41         return;
42     }
43     else if (count == k || pow(begin,p)>n || sum2>n) return;
44     tmp.push_back(begin);
45     DFS(begin, sum2 + pow(begin, p), count + 1);
46     tmp.pop_back();
47     DFS(begin + 1, sum2, count);
48 
49     tmp.push_back(begin);
50     DFS(begin + 1, sum2 + pow(begin, p), count + 1);
51     tmp.pop_back();
52 }
53 
54 int main() {
55     scanf("%d%d%d", &n, &k, &p);
56     DFS(1, 0, 0);
57     if (ans.size() != 0) {
58         printf("%d =", n);
59         for (int i = ans.size()-1; i >=0; i--) {
60             printf(" %d^%d", ans[i], p);
61             if (i != 0) printf(" +");
62         }
63     }
64     else {
65         printf("Impossible");
66     }
67 }

(2)参考答案

抄一遍答案吸取下经验

 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 //n、k、p如题所述,maxFacSum记录最大底数之和
 7 int n, k, p, maxFacSum = -1;
 8 //fac记录0^p,1^p...i^p,使得i^p为不超过n的最大数
 9 //ans存放最优底数序列,temp存放递归中的临时底数序列
10 vector<int> fac, ans, temp;
11 //power函数计算x^p
12 int power(int x) {
13     int ans = 1;
14     for (int i = 0; i < p; i++) {
15         ans *= x;
16     }
17     return ans;
18 }
19 //init函数预处理fac数组,注意把0也存进去。
20 void init() {
21     int i = 0, temp = 0;
22     while (temp <= n) {
23         fac.push_back(temp);
24         temp = power(++i);
25     }
26 }
27 //DFS函数,当前访问fac[index],nowK为当前选中个数
28 //sum为当前选中的数之和
29 void DFS(int index, int nowK, int sum, int facSum) {
30     if (sum == n && nowK == k) { //找到一个满足的序列
31         if (facSum > maxFacSum) {
32             ans = temp;
33             maxFacSum = facSum;
34         }
35         return;
36     }
37     if (sum > n || nowK > k) return;
38     if (index - 1 >= 0) {
39         temp.push_back(index);
40         DFS(index, nowK + 1, sum + fac[index], facSum + index); //“选”的分支
41         temp.pop_back(); //“选”的分支结束后把刚加进去的数pop掉
42         DFS(index - 1, nowK, sum, facSum); //不选的分支
43         //分支是由选不选组成的
44     }
45 }
46 
47 int main() {
48     scanf("%d%d%d", &n, &k, &p);
49     init();
50     DFS(fac.size() - 1, 0, 0, 0);
51     if (maxFacSum == -1) printf("Impossible\n");
52     else {
53         printf("%d = %d^%d", n, ans[0], p);
54         for (int i = 1; i < ans.size(); i++) {
55             printf(" + %d^%d", ans[i], p);
56         }
57     }
58 }

 

A1091

(1)自己尝试

 1 #include <cstdio>
 2 #include <queue>
 3 using namespace std;
 4 
 5 int mind[70][1300][140];
 6 bool inQue[70][1300][140];
 7 
 8 int X[6] = { 1,-1,0,0,0,0 };
 9 int Y[6] = { 0,0,1,-1,0,0 };
10 int Z[6] = { 0,0,0,0,1,-1 };
11 
12 int m, n, l, t;
13 
14 struct Node {
15     int x, y, z;
16     int step;
17 }node;
18 
19 bool judge(Node Q) {
20     if (Q.x >= m || Q.x < 0 || Q.y >= n || Q.y < 0 || Q.z >= l || Q.z < 0) return false;
21     if (inQue[Q.z][Q.x][Q.y] == true) return false;
22     if (mind[Q.z][Q.x][Q.y] == 0) return false;
23     else if(mind[Q.z][Q.x][Q.y] == 1) return true;
24 }
25 
26 int BFS(int x,int y,int z) {
27     queue<Node> que;
28     Node Q;
29     Q.x = x;
30     Q.y = y;
31     Q.z = z;
32     Q.step = 0;
33     que.push(Q);
34     inQue[Q.z][Q.x][Q.y] = true;
35     int count = 1;
36     while (!que.empty()) {
37         Q = que.front();
38         que.pop();
39         for (int i = 0; i < 6; i++) {
40             Node Q1;
41             Q1.x = Q.x + X[i];
42             Q1.y = Q.y + Y[i];
43             Q1.z = Q.z + Z[i];
44             Q1.step = Q.step + 1;
45             if (judge(Q1)) {
46                 que.push(Q1);
47                 inQue[Q1.z][Q1.x][Q1.y] = true;
48                 count++;
49             }
50         }
51     }
52     return count;
53 }
54 
55 int main() {
56 
57     scanf("%d%d%d%d", &m, &n, &l, &t);
58     for (int z = 0; z < l; z++) {
59         for (int x = 0; x < m; x++) {
60             for (int y = 0; y < n; y++) {
61                 scanf("%d", &mind[z][x][y]);
62             }
63         }
64     }
65     int ans = 0;
66     int count = 0;
67     for (int z = 0; z < l; z++) {
68         for (int x = 0; x < m; x++) {
69             for (int y = 0; y < n; y++) {
70                 Node Q;
71                 Q.x = x;
72                 Q.y = y;
73                 Q.z = z;
74                 if(judge(Q)) count = BFS(x, y, z);
75                 if (count >= t) ans+=count;
76                 count =0;
77             }
78         }
79     }
80     printf("%d\n", ans);
81 }

用VSdebug 就硬De 总会过的……大概。

 

A1020

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 50;
 8 struct node {
 9     int data;
10     node* lchild;
11     node* rchild;
12 };
13 
14 int pre[maxn], in[maxn], post[maxn]; //先序,中序,后续
15 int n;
16 
17 node* create(int postL, int postR, int inL, int inR) {
18     if (postL > postR) {
19         return NULL;
20     }
21     node* root = new node;
22     root->data = post[postR];
23     int k;
24     for (k = inL; k <= inR; k++) {
25         if (post[postR] == in[k]) break;
26     }
27     
28     int numLeft = k - inL;
29     root->lchild = create(postL, postL + numLeft - 1, inL, k-1);
30     root->rchild = create(postL + numLeft, postR-1, k + 1, inR);
31     return root;
32 }
33 
34 int num = 0; //已输出的结点个数
35 void BFS(node* root) {
36     queue<node*> q;
37     q.push(root);
38     while (!q.empty()) {
39         node* now = q.front();
40         q.pop();
41         printf("%d", now->data);
42         num++;
43         if (num < n) printf(" ");
44         if (now->lchild != NULL) q.push(now->lchild); //本身就是个指针,不用提领。
45         if (now->rchild != NULL) q.push(now->rchild);
46     }
47 }
48 int main() {
49     scanf("%d", &n);
50     for (int i = 0; i < n; i++) {
51         scanf("%d", &post[i]);
52     }
53     for (int i = 0; i < n; i++) {
54         scanf("%d", &in[i]);
55     }
56     node* root = create(0, n - 1, 0, n - 1);
57     BFS(root);
58 }

注意看明白每种遍历的值,先序和层次第一个为根,后序最后一个为根,所以建树递归的时候也有不同。

 

A1086

 1 #include <cstdio>
 2 #include <string>
 3 #include <stack>
 4 #include <iostream>
 5 using namespace std;
 6 //push的次序就是先序遍历的次序,pop的次序就是后续遍历的次序。
 7 struct node {
 8     int data;
 9     node* lchild;
10     node* rchild;
11 };
12 
13 int pre[50], in[50], post[50];
14 stack<int> stk;
15 int num = 0;
16 
17 node* create(int preL, int preR, int inL, int inR) {
18     if (preL > preR) {
19         return NULL;
20     }
21     node* root = new node;
22     root->data = pre[preL];
23     int i;
24     for (i = inL; i <= inR; i++) {
25         if (in[i] == pre[preL]) break;
26     }
27     int numLeft = i - inL;
28     root->lchild = create(preL + 1, preL + numLeft, inL, i - 1);
29     root->rchild = create(preL + numLeft + 1, preR, i + 1, inR);
30     return root;
31 }
32 
33 void postTvl(node* root,int n) {
34     if (root == NULL) return;
35     postTvl(root->lchild,n);
36     postTvl(root->rchild,n);
37     printf("%d", root->data);
38     num++;
39     if (num != n) printf(" ");
40 }
41 
42 int main() {
43     int n;
44     scanf("%d",&n);
45     int preId = 0, inId = 0;
46     for (int i = 0; i < 2 * n; i++) {
47         string a;
48         cin >> a;
49         if (a == "Push") {
50             scanf("%d", &pre[preId]);
51             stk.push(pre[preId]);
52             preId++;
53         }
54         else {
55             int tmp = stk.top();
56             stk.pop();
57             in[inId] = tmp;
58             inId++;
59         }
60     }
61     node* root = create(0, n - 1, 0, n - 1);
62     postTvl(root, n);
63 }

只要知道入栈是先序遍历,出栈是中序遍历就OK了。

 

A1020

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 
 6 const int maxn =20;
 7 
 8 int count1 = 0;
 9 
10 struct node
11 {
12     int data;
13     int lchild, rchild;
14 }Node[maxn];
15 
16 bool hasRoot[maxn];
17 
18 void reverse(int p) {
19     if (p == -1) return;
20     reverse(Node[p].lchild);
21     reverse(Node[p].rchild);
22     int tmp = Node[p].lchild;
23     Node[p].lchild = Node[p].rchild;
24     Node[p].rchild = tmp;
25 }
26 
27 void levelOrder(int root,int n) {
28     queue<int> q;
29     q.push(root);
30     int count = 0;
31     while (!q.empty()) {
32         int now = q.front();
33         q.pop();
34         count++;
35         printf("%d", now);
36         if (count != n) printf(" ");
37         else printf("\n");
38         if (Node[now].lchild != -1) {
39             q.push(Node[now].lchild);
40         }
41         if (Node[now].rchild != -1) {
42             q.push(Node[now].rchild);
43         }
44     }
45 }
46 
47 void inOrder(int root, int n) {
48     if (root == -1) return;
49     inOrder(Node[root].lchild,n);
50     printf("%d", root);
51     count1++;
52     if (count1 != n) printf(" ");
53     inOrder(Node[root].rchild, n);
54 }
55 
56 int main() {
57     int n;
58     scanf("%d", &n);
59     getchar();
60     for (int i = 0; i < n; i++) {
61         node now;
62         char l, r;
63         scanf("%c %c%*c", &l, &r);
64         if (l != '-') {
65             now.lchild = l - '0';
66             hasRoot[l - '0'] = true;
67         }
68         else now.lchild = -1;
69         if (r != '-') {
70             now.rchild = r - '0';
71             hasRoot[r - '0'] = true;
72         }
73         else now.rchild = -1;
74         now.data = i;
75         Node[i] = now;
76     }
77     int k;
78     for (k = 0; k < n; k++) {
79         if (!hasRoot[k]) break;
80     }
81     reverse(k);
82     levelOrder(k, n);
83     inOrder(k, n);
84 }

静态链表,反转后序遍历即可,然后判断是否有根需要读入的时候记录。正经二叉树可做不到。。。

 

A1053

 1 #include <cstdio>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int maxn = 110;
 7 
 8 int n, m, s; //输入第一行
 9 
10 struct node {
11     int id;
12     int weight;
13     vector<int> child;
14 }Node[maxn];
15 
16 int sum = 0;
17 vector<vector<int> > ans;
18 vector<int> tmpAns;
19 
20 void DFS(int root,int sum) {
21     sum += Node[root].weight;
22     if (Node[root].child.size() == 0 && sum == s) {
23         tmpAns.push_back(Node[root].weight);
24         ans.push_back(tmpAns);
25         tmpAns.pop_back();
26     }
27     if (sum > s) return;
28     for (int i = 0; i < Node[root].child.size(); i++) {
29         tmpAns.push_back(Node[root].weight);
30         DFS(Node[root].child[i],sum);
31         tmpAns.pop_back();
32     }
33 }
34 
35 bool cmp(vector<int> a, vector<int> b) {
36     return a > b;
37 }
38 
39 int main() {
40     scanf("%d%d%d", &n, &m, &s);
41     for (int i = 0; i < n; i++) {
42         scanf("%d", &Node[i].weight);
43     }
44     for (int i = 0; i < m; i++) {
45         int tmp,n2;
46         scanf("%d%d", &tmp,&n2);
47         for (int j = 0; j < n2; j++) {
48             int c;
49             scanf("%d", &c);
50             Node[tmp].child.push_back(c);
51         }
52     }
53     DFS(0, sum);
54     sort(ans.begin(), ans.end(), cmp);
55     for (int i = 0; i < ans.size(); i++) {
56         for (int j = 0; j < ans[i].size(); j++) {
57             printf("%d", ans[i][j]);
58             if (j!=ans[i].size() - 1) printf(" ");
59             else printf("\n");
60         }
61     }
62 }

 

A1079

 1 #include <cstdio>
 2 #include <vector>
 3 using namespace std;
 4 
 5 const int maxn = 100010;
 6 
 7 struct node
 8 {
 9     int count;
10     vector<int> child;
11 }Node[maxn];
12 
13 
14 int n; //结点个数
15 double p, r;
16 double ans = 0;
17 
18 void DFS(int root,double p) {
19     if (Node[root].child.size() == 0) {
20         ans += p*Node[root].count;
21     }
22     for (int i = 0; i < Node[root].child.size(); i++) {
23         DFS(Node[root].child[i], p*(1 + r / 100));
24     }
25 }
26 
27 int main() {
28     scanf("%d%lf%lf", &n, &p, &r);
29     for (int i = 0; i < n; i++) {
30         int tmp;
31         scanf("%d", &tmp);
32         if (tmp == 0) scanf("%d", &Node[i].count);
33         else {
34             for (int j = 0; j < tmp; j++) {
35                 int tmp2;
36                 scanf("%d", &tmp2);
37                 Node[i].child.push_back(tmp2);
38             }
39         }
40     }
41     DFS(0, p);
42     printf("%.1f", ans);
43     
44 }

小心二重循环!

 

A1090

 1 #include <cstdio>
 2 #include <vector>
 3 using namespace std;
 4 
 5 const int maxn = 100010;
 6 
 7 struct node
 8 {
 9     int count;
10     vector<int> child;
11 }Node[maxn];
12 
13 
14 int n; //结点个数
15 double p, r;
16 int root;
17 double max1 = -1;
18 int ans = 0;
19 
20 void DFS(int root, double p) {
21     if (Node[root].child.size() == 0) {
22         if (p > max1) {
23             max1 = p;
24             ans = 1;
25         }
26         else if (p == max1) ans++;
27     }
28     for (int i = 0; i < Node[root].child.size(); i++) {
29         DFS(Node[root].child[i], p*(1 + r / 100));
30     }
31 }
32 
33 int main() {
34     scanf("%d%lf%lf", &n, &p, &r);
35     for (int i = 0; i < n; i++) {
36         int tmp;
37         scanf("%d", &tmp);
38         if (tmp == -1) root = i;
39         else {
40             Node[tmp].child.push_back(i);
41         }
42     }
43     DFS(root, p);
44     printf("%.2f %d", max1, ans);
45 
46 }

上边题改改就好了。

 

A1094

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <queue>
 5 using namespace std;
 6 
 7 const int maxn = 110;
 8 
 9 struct node
10 {
11     int id;
12     vector<int> child;
13     int step;
14 }Node[maxn];
15 
16 int n, m;
17 
18 
19 
20 void BFS(int root,int count[]) {
21     queue<int> Q;
22     Node[root].step = 1;
23     Q.push(root);
24     while (!Q.empty()) {
25         int front = Q.front();
26         count[Node[front].step]++;
27         Q.pop();
28         for (int i = 0; i < Node[front].child.size(); i++) {
29             int tmp = Node[front].child[i];
30             Node[tmp].step = Node[front].step + 1;
31             Q.push(tmp);
32         }
33     }
34 }
35 
36 int findMax(int count[], int &a) {
37     int max = 0;
38     for (int i = 1; i < maxn; i++) {
39         if (count[i] > max) {
40             max = count[i];
41             a = i;
42         }
43     }
44     return max;
45 }
46 
47 int main() {
48     scanf("%d%d", &n, &m);
49     for (int i = 1; i <= m; i++) {
50         int tmp,n2;
51         scanf("%d%d", &tmp,&n2);
52         for (int j = 0; j < n2; j++) {
53             int tmp2;
54             scanf("%d", &tmp2);
55             Node[tmp].child.push_back(tmp2);
56         }
57     }
58     int count[maxn];
59     for(int i=0;i<maxn;i++){
60         count[i]= 0;
61     }
62     BFS(1, count);
63     int a = 0;
64     int max = findMax(count, a);
65     printf("%d %d", max, a);
66 }

同样没什么好说的,套路。

 

A1106

 1 #include <cstdio>
 2 #include <vector>
 3 using namespace std;
 4 
 5 const int maxn = 100010;
 6 
 7 struct node
 8 {
 9     int count;
10     vector<int> child;
11 }Node[maxn];
12 
13 
14 int n; //结点个数
15 double p, r;
16 int root;
17 double max1 = 1e10+1;
18 int ans = 0;
19 
20 void DFS(int root, double p) {
21     if (Node[root].child.size() == 0) {
22         if (p < max1) {
23             max1 = p;
24             ans = 1;
25         }
26         else if (p == max1) ans++;
27     }
28     for (int i = 0; i < Node[root].child.size(); i++) {
29         DFS(Node[root].child[i], p*(1 + r / 100));
30     }
31 }
32 
33 int main() {
34     scanf("%d%lf%lf", &n, &p, &r);
35     for (int i = 0; i < n; i++) {
36         int tmp;
37         scanf("%d", &tmp);
38         if (tmp != 0) {
39             for (int j = 0; j < tmp; j++) {
40                 int tmp2;
41                 scanf("%d", &tmp2);
42                 Node[i].child.push_back(tmp2);
43             }
44         }
45 
46     }
47     DFS(0, p);
48     printf("%.4f %d", max1, ans);
49 
50 }

接着改改上边题就好了 。

 

A1004

接着改上边 

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <queue>
 5 using namespace std;
 6 
 7 const int maxn = 110;
 8 
 9 struct node
10 {
11     int id;
12     vector<int> child;
13     int step;
14 }Node[maxn];
15 
16 int n, m;
17 
18 
19 
20 int BFS(int root, int count[]) {
21     queue<int> Q;
22     Node[root].step = 1;
23     Q.push(root);
24     int front;
25     while (!Q.empty()) {
26         front = Q.front();
27         if(Node[front].child.size()==0) count[Node[front].step]++;
28         Q.pop();
29         for (int i = 0; i < Node[front].child.size(); i++) {
30             int tmp = Node[front].child[i];
31             Node[tmp].step = Node[front].step + 1;
32             Q.push(tmp);
33         }
34     }
35     return Node[front].step;
36 }
37 
38 
39 int main() {
40     scanf("%d%d", &n, &m);
41     for (int i = 1; i <= m; i++) {
42         int tmp, n2;
43         scanf("%d%d", &tmp, &n2);
44         for (int j = 0; j < n2; j++) {
45             int tmp2;
46             scanf("%d", &tmp2);
47             Node[tmp].child.push_back(tmp2);
48         }
49     }
50     int count[maxn];
51     for (int i = 0; i < maxn; i++) {
52         count[i] = 0;
53     }
54     int num = BFS(1, count);
55     for (int i = 1; i <= num; i++) {
56         if (i != num) printf("%d ", count[i]);
57         else printf("%d", count[i]);
58     }
59 }

 

A1043

这题暂时过了,第二个测试点过不了。

(1)参考答案

 1 #include <cstdio>
 2 #include <vector>
 3 using namespace std;
 4 struct node
 5 {
 6     int data;
 7     node* left;
 8     node* right;
 9 };
10 
11 void insert(node* &root, int data) {
12     if (root == NULL) {
13         root = new node;
14         root->data = data;
15         root->left = root->right = NULL;
16         return;
17     }
18     if (data < root->data) insert(root->left, data);
19     else insert(root->right, data);
20 }
21 void preOrder(node* root, vector<int>& vi) {
22     if (root == NULL) return;
23     vi.push_back(root->data);
24     preOrder(root->left, vi);
25     preOrder(root->right, vi);
26 }
27 
28 void preOrderMirror(node* root, vector<int>& vi) {
29     if (root == NULL) return;
30     vi.push_back(root->data);
31     preOrderMirror(root->right, vi);
32     preOrderMirror(root->left, vi);
33 }
34 
35 void postOrder(node* root, vector<int>& vi) {
36     if (root == NULL) return;
37     postOrder(root->left, vi);
38     postOrder(root->right, vi);
39     vi.push_back(root->data);
40 }
41 
42 void postOrderMirror(node* root, vector<int>& vi) {
43     if (root == NULL) return;
44     postOrderMirror(root->right,vi);
45     postOrderMirror(root->left,vi);
46     vi.push_back(root->data);
47 }
48 
49 vector<int> origin, pre, preM, post, postM;
50 int main() {
51     int n, data;
52     node* root = NULL;
53     scanf("%d", &n);
54     for (int i = 0; i < n; i++) {
55         scanf("%d", &data);
56         origin.push_back(data);
57         insert(root, data);
58     }
59     preOrder(root, pre);
60     preOrderMirror(root, preM);
61     postOrder(root, post);
62     postOrderMirror(root, postM);
63     if (origin == pre) {
64         printf("YES\n");
65         for (int i = 0; i < post.size(); i++) {
66             printf("%d",post[i]);
67             if (i < post.size() - 1) printf(" ");
68 
69         }
70     }
71     else if (origin == preM) {
72         printf("YES\n");
73         for (int i = 0; i < postM.size(); i++) {
74             printf("%d", postM[i]);
75             if (i < postM.size() - 1) printf(" ");
76         }
77     }
78     else {
79         printf("NO\n");
80     }
81 }

通过给定的插入序列,构建出二叉排序树。对镜像术的先序遍历只需要在原树的线序白能力时交换左右子树的访问顺序即可。

 

A1064

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn = 1010;
 5 //n为结点数,number用来存放结点权值,CBT用以存放完全二叉树
 6 //index从小到大枚举number数组
 7 int n, number[maxn], CBT[maxn],index = 0;
 8 void inOrder(int root) {
 9     if (root > n) return;
10     inOrder(root * 2);
11     CBT[root] = number[index++];
12     inOrder(root * 2 + 1);
13 }
14 int main() {
15     scanf("%d", &n);
16     for (int i = 0; i < n; i++) {
17         scanf("%d", &number[i]);
18     }
19     sort(number, number + n); 
20     inOrder(1);
21     for (int i = 1; i <= n; i++) {
22         printf("%d", CBT[i]);
23         if (i < n) printf(" ");
24     }
25     return 0;
26 }

如果使用数组存放完全二叉树,那么对完全二叉树当中的任何一个结点(设编号为x,其中根节点编号为1),其左孩子结点的编号一定是2x,而右孩子结点的编号一定是2x+1。按层序存放完全二叉树。

 

//中序遍历,将排好序的序列依次填入二叉树结点。

遍历访问的是指针,排好序的是data,所以按照中序遍历的输出结果的data填入二叉树只要还原中序遍历过程即可。

 

posted @ 2021-03-25 14:21  魂淡菌  阅读(72)  评论(0)    收藏  举报