当前位置:网站首页>PAT 刷题笔记
PAT 刷题笔记
2022-08-07 11:10:00 【相约相守到天边】
2022.8.2
C++ 时分秒转换
#include <iostream>
using namespace std;
int main()
{
int val;
cin >> val;
int hh = val / 3600;
int mm = val / 60 % 60;
int ss = val % 60;
printf("%02d:%02d:%02d\n", hh, mm, ss);
return 0;
}
2022.8.3
树与二叉树
#include <iostream>
#include <queue>
#include <map>
using namespace std;
struct node
{
int data;
node *left;
node *right;
}; // 定义二叉树节点
const int N = 31;
int n, in[N], pre[N], post[N];
// 已知后序遍历和中序遍历,求前序遍历
void preOrder(int root, int start, int end)
{
if (start > end)
{
return;
}
int i = start; // 保存根节点在中序序列中的位置
while (i < end && in[i] != post[root])
{
i++;
}
printf("%d ", post[root]); // 中左右
preOrder(root - (end - i + 1), start, i - 1); // 画图
preOrder(root - 1, i + 1, end);
}
// 已知前序遍历和中序遍历,求后序遍历
void postOrder(int root, int start, int end)
{
if (start > end)
{
return;
}
int i = start; // 保存根节点在中序序列中的位置
while (i < end && in[i] != pre[root])
{
i++;
}
postOrder(root + 1, start, i - 1); // 画图
postOrder(root + 1 + i - start, i + 1, end);
printf("%d ", pre[root]); //左右中
}
node *createTree(int postL, int postR, int inL, int inR)
{
if (postL > postR)
{
return nullptr;
}
node *root = new node;
root->data = post[postR];
int k; //求根节点在中序序列中的位置
for (k = inL; k <= inR; k++)
{
if (in[k] == post[postR])
{
break;
}
}
int num_left = k - inL; // 左子树节点个数
root->left = createTree(postL, postL + num_left - 1, inL, k - 1);
root->right = createTree(postL + num_left, postR - 1, k + 1, inR);
return root; // 返回根节点
}
void bfs(node *root)
{
queue<node *> q;
node *p = root;
q.push(p);
int num = 0; //记录已访问节点个数
while (!q.empty())
{
p = q.front();
q.pop();
num++;
printf("%d", p->data); // 访问p节点
if (num < n)
{
printf(" ");
}
if (p->left != nullptr)
{
q.push(p->left);
}
if (p->right != nullptr)
{
q.push(p->right);
}
}
printf("\n");
}
// 方法二
map<int, int> level;
void pre2(int root, int start, int end, int idx)
{
if (start > end)
{
return;
}
int i = start;
while (i < end && in[i] != post[root])
{
i++;
}
level[idx] = post[root]; // visit;
pre2(root - (end - i + 1), start, i - 1, 2 * idx + 1);
pre2(root - 1, i + 1, end, 2 * idx + 2);
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
scanf("%d", &post[i]);
}
for (int i = 0; i < n; i++)
{
scanf("%d", &in[i]);
}
// 方法一
// node *root = createTree(0, n - 1, 0, n - 1);
// bfs(root); //层次序遍历
// 方法二
pre2(n - 1, 0, n - 1, 0);
auto it = level.begin();
printf("%d", it->second);
while (++it != level.end())
{
printf(" %d", it->second);
}
return 0;
}
边栏推荐
- uboot代码分析
- UGUI系列-Dropdown控件研究(Unity3D)
- Advanced optimization techniques for project optimization (Unity3D)
- xlwings模块(数据保存为xlsx文件)
- pyautogui实践——10行代码实现《破事精英》里面的“凝固的桌面“
- The non-professional AI brother is on fire: he doesn't have an ML degree, but he got an offer from DeepMind
- pyautogui practice - 10 lines of code to realize the "solidified desktop" in "Broken Elite"
- 《bug记录》在利用TCP协议创建【服务器-客户端交互程序】中出现的一些问题
- Shell 文本三剑客 grep
- UGUI series-InputField limits the number of inputs and limits the input format
猜你喜欢

Shell Text Three Musketeers grep

社群营销变现模式揭秘!如何用社群卖出自己的产品?

Flask Framework - Application Error Handling

Talk about automatic power switching circuit (summary of common automatic switching circuits)

一个很重的代码坏味:多行代码糅合在一行

uniapp本地插件列表为空的问题

xlwings模块(数据保存为xlsx文件)

NProgress plugin (progress bar)

unexpected pos 53504 vs 53392

一文讲清-DeFI王者AAVE最新的稳定币GHO提案
随机推荐
CCNA--GNS3仿真环境的搭建及SecureCRT8.7程序控制台美化调试
项目优化之循环优化(Unity3D)
ES6之原型
csv module
项目优化之Canvas优化(Unity3D)
有趣的特性:CHECK约束
力扣 1408. 数组中的字符串匹配
Numble 游戏规则
社群营销变现模式揭秘!如何用社群卖出自己的产品?
uboot代码分析
PG core technology articles--MVCC
UGUI系列-鼠标移动到按钮上显示信息(Unity3D)
CCNA-Installing SecureCRT 8.7 with GNS3 Emulation
Unity packages webgl and deploys it to the local web server
产品“白送”+溢价入股,赛诺菲打响抄底中国生物科技资产第一枪
Leek 1408. String matching in arrays
xlwings module (data is saved as xlsx file)
如果想学一门语言,需要许多前趋知识点
Swin_Unet & Trans_UNet & Unet & Deeplabv3网络训练结果对比
UGUI series - screen adaptive multiple allocation rate adaptation (Untiy3D)