PassThePAT


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

1028 List Sorting

发表于 2020-05-30

1027 Colors in Mars

发表于 2020-05-30

1026 Table Tennis

发表于 2020-05-30

1025 PAT Ranking

发表于 2020-05-30

1024 Palindromic Number

发表于 2020-05-30

1023 Have Fun with Numbers

发表于 2020-05-30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;

int main() {
int book[10] = {0};
char s[50];
scanf("%s", s);
int flag1 = 0; // 是否进一位
int flag = 1; // 是否是原数字的重新排列
int len = strlen(s);
for (int i = len - 1; i >= 0; i--) {
int t = s[i] - '0';
book[t]++;
t = t * 2 + flag1;
flag1 = 0;
if (t >= 10) {
flag1 = 1;
t = t - 10;
}
s[i] = t + '0';
book[t]--;
}
for (int i = 0; i < 10; i++) {
if (book[i] != 0) {
flag = 0;
break;
}
}
printf("%s", (flag1 == 1 || flag == 0) ? "No\n" : "Yes\n");
if (flag1) printf("1");
printf("%s", s);

return 0;
}


123456789
Yes
246913578

1022 Digital Library

发表于 2020-05-30

1021 Deepest Root

发表于 2020-05-30

1020 Tree Traversals

发表于 2020-05-30

定义结构体变量时,关键字struct可以省略吗?
在C++中可以bai,但在duc中不行zhi。

1
2
3
4
5
6
7
8
9
10
11
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;

struct LNode {
ElemType data;
struct LNode *next;
};
typedef struct LNode LNode;
typedef struct LNode* LinkList;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;

const int maxn = 50;

int n, post[maxn], in[maxn];

typedef struct LNode {
int data;
struct LNode* lc;
struct LNode* rc;
}LNode, *Tree;

Tree creatTree(int postL, int postR, int inL, int inR) {
if (postL > postR || inL > inR) return NULL;
LNode* root = new LNode;
root->data = post[postR];
int indexRoot;
for (indexRoot = inL; indexRoot <= inR; indexRoot++) {
if (in[indexRoot] == post[postR])
break;
}
int lengthOfLeftTree = indexRoot - inL;
root->lc = creatTree(postL, postL + lengthOfLeftTree - 1, inL, indexRoot - 1);
root->rc = creatTree(postL + lengthOfLeftTree, postR - 1, indexRoot + 1, inR);
return root;
}

void BFS(Tree root) {
int num = 0;
queue<LNode*> q;
q.push(root);
while (!q.empty()) {
LNode* now = q.front();
q.pop();
printf("%d", now->data);
num++;
if (num < n) printf(" ");
if (now->lc != NULL) q.push(now->lc);
if (now->rc != NULL) q.push(now->rc);
}
}

int main() {
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]);
}
// 中序
Tree root = creatTree(0, n - 1, 0, n - 1);
BFS(root);

return 0;
}


7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

4 1 6 3 5 7 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
int index, value;
};
bool cmp(node a, node b) {
return a.index < b.index;
}
vector<int> post, in;
vector<node> ans;
void pre(int root, int start, int end, int index) {
if (start > end) return;
int i = start;
while (i < end && in[i] != post[root]) i++;
ans.push_back({index, post[root]});
pre(root - 1 - end + i, start, i - 1, 2 * index + 1);
pre(root - 1, i + 1, end, 2 * index + 2);
}
int main() {
int n;
scanf("%d", &n);
post.resize(n);
in.resize(n);
for (int i = 0; i < n; i++) scanf("%d", &post[i]);
for (int i = 0; i < n; i++) scanf("%d", &in[i]);
pre(n - 1, 0, n - 1, 0);
sort(ans.begin(), ans.end(), cmp);
for (int i = 0; i < ans.size(); i++) {
if (i != 0) cout << " ";
cout << ans[i].value;
}
return 0;
}

1019 General Palindromic Number

发表于 2020-05-30

备注:arr数组里是从低位到高位的数字。所以输出得从后到前。十进制转b进制也是取余。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>
using namespace std;

int main() {
int n, b;
scanf("%d %d", &n, &b);
int arr[100] = {0}, len = 0;
while (n) {
arr[len++] = n % b;
n /= b;
}
bool flag = true;
for (int i = 0; i < len / 2; i++) {
if (arr[i] != arr[len - 1 - i]) {
printf("No\n");
flag = false;
break;
}
}
if (flag) printf("Yes\n");
for (int i = len - 1; i >= 0; i--) {
printf("%d", arr[i]);
if (i) printf(" ");
}

return 0;
}


27 2
Yes
1 1 0 1 1
1…13141516

e5

158 日志
5 标签
© 2022 e5
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4