码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • pta数据结构day9


    总结:写了三题,花费半天,准备先把leetbook的二叉树先写一遍,因为pta没有最优解,写起来逻辑并不顺畅。

    先看树种统计,比较另外2题简单;

    题目详情 - 案例4-1.6 树种统计 (pintia.cn)

    1. #include<iostream>
    2. #include<map>
    3. #include<string>
    4. using namespace std;
    5. int main() {
    6. map<string, int>mp;
    7. int n;
    8. cin >> n;
    9. cin.get();//blank
    10. string s;
    11. for (int i = 0; i < n; i++) {
    12. getline(cin, s);
    13. map<string, int>::iterator it = mp.find(s);//原来有这个元素,统计个数+1;
    14. if (it != mp.end()) {
    15. it->second += 1;
    16. }
    17. else {//没有这个元素,创建并初始个数为1
    18. mp.insert(make_pair(s, 1));
    19. }
    20. }
    21. cout.setf(ios_base::fixed);
    22. cout.precision(4);
    23. for (map<string, int>::iterator i = mp.begin(); i != mp.end(); i++) {
    24. cout << i->first << ' ' << (i->second)*100.0/n<<'%' << endl;
    25. }
    26. return 0;
    27. }

    第二题是树的同构,考察递归的逻辑,可以参考mooc视频多写几次;

    题目详情 - 基础实验4-2.1 树的同构 (pintia.cn) 

    1. #include<iostream>
    2. using namespace std;
    3. #define NULL -1
    4. struct node {
    5. char data;
    6. int left, right;
    7. }p[10],q[10];
    8. int input(node p[],int n) {
    9. int check[10]={0},root=NULL;
    10. char ch;
    11. for (int i = 0; i < n; i++) {
    12. cin.get();//blank
    13. cin >> p[i].data;
    14. cin >> ch;
    15. if (ch == '-') {
    16. p[i].left = -1;
    17. }
    18. else {
    19. p[i].left = ch - '0';
    20. check[p[i].left] = 1;
    21. }
    22. cin >> ch;
    23. if (ch == '-') {
    24. p[i].right = -1;
    25. }
    26. else {
    27. p[i].right = ch - '0';
    28. check[p[i].right] = 1;
    29. }
    30. }
    31. for (int i = 0; i < n; i++) {
    32. if (check[i] == 0) {
    33. root = i;
    34. }
    35. }
    36. return root;
    37. }
    38. bool is_tonggou(int r1,int r2) {
    39. if (r1 == NULL && r2 == NULL) {
    40. return 1;
    41. }
    42. else if ((r1 == NULL && r2 != NULL) || (r1 != NULL && r2 == NULL)) {
    43. return 0;
    44. }
    45. else if (p[r1].data != q[r2].data) {
    46. return 0;
    47. }
    48. else if ((p[r1].left == NULL) && (q[r2].left == NULL)) {
    49. return is_tonggou(p[r1].right, q[r2].right);
    50. }
    51. else if ((p[r1].left != NULL) && (q[r2].left != NULL)&&(p[p[r1].left].data==q[q[r2].left].data)) {
    52. return is_tonggou(p[r1].left, q[r2].left) && is_tonggou(p[r1].right, q[r2].right);
    53. }
    54. else {
    55. return is_tonggou(p[r1].left, q[r2].right) && is_tonggou(p[r1].right, q[r2].left);
    56. }
    57. }
    58. int main() {
    59. int n,m,root1,root2;
    60. cin >> n;
    61. root1=input(p,n);
    62. cin >> m;
    63. if (n != m) {
    64. cout << "No\n";
    65. return 0;
    66. }
    67. root2=input(q,m);
    68. bool flag = is_tonggou(root1,root2);
    69. if (flag) {
    70. cout << "Yes\n";
    71. }
    72. else {
    73. cout << "No\n";
    74. }
    75. return 0;
    76. }

    最后是目录树,比较复杂,需要再写一次;

    题目详情 - 基础实验4-2.6 目录树 (pintia.cn) 

     

     

     

  • 相关阅读:
    QT:工业软件开发的首选“
    九、Spring Boot 缓存(1)
    Android Automotive (一) 介绍
    从企业的视角来看,数据中台到底意味着什么?
    大数据基础设施搭建 - ZooKeeper
    Python Matplotlib legend函数:为每条折线添加图例
    尚品汇_第6章_商品详情页
    流媒体技术优化
    【博学谷学习记录】超强总结,用心分享丨人工智能 Python基础 个人学习总结之列表排序
    MVC架构回顾
  • 原文地址:https://blog.csdn.net/weixin_58073817/article/details/126910806
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号