码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 华为机试C语言-找到比自己强的人数


    题目描述:https://pycoder.blog.csdn.net/article/details/127216746?spm=1001.2014.3001.5502
    https://blog.csdn.net/qq_27695659/article/details/125882686

    这道题一直再想有没有更好的方法,很遗憾,没能想出来。因为相邻的两层递归无法建立联系,当前老师比自己强的人数无法从徒弟的身上找到答案,中间的减枝无法去掉,不清楚是否会超时,时间复杂度还是挺高的。供大家参考吧,有更好的解法欢迎大家赐教~

    #include 
    #include 
    #include 
    #include 
    
    int dfs(int map[100][2], int size, int target, int teacher, int cnt) {
        for (int i = 0; i < size; i++) {
            if (map[i][0] == teacher) {
                if (map[i][1] < target) {
                    cnt++;
                }
                cnt += dfs(map, size, target, map[i][1], 0);
            }
        }
    
        return cnt;
    }
    
    int main(void) {
        char str[1000] = {0};
        int row = 0, col = 0;
        int index = 0;
        int map[100][2] = {0};
        int max = 0;
        int res[100];
    
        scanf("%s", str);
    
        int left, right, len;
        char node[100];
        char* tmp;
        int teacher, student;
        for (int i = 1; i < strlen(str) - 1; i++) {
            if (str[i] == '[') {
                left = i + 1;
            } else if (str[i] == ']') {
                right = i - 1;
                len = right - left + 1;
                strncpy(node, &str[left], len);
                node[len] = '\0';
                printf("%s\n", node);
    
                tmp = strtok(node, ",");
                teacher = atoi(tmp);
                tmp = strtok(NULL, ",");
                student = atoi(tmp);
    
                map[index][0] = teacher;
                map[index++][1] = student;
                max = fmax(max, teacher);
                max = fmax(max, student);
            }
        }
    
        for (int i = 1; i <= max; i++) {
            res[i] = dfs(map, index, i, i, 0);
        }
    
        printf("[");
        for (int i = 1; i < max; i++) {
            printf("%d,", res[i]);
        }
        printf("%d]\n", res[max]);
    
    
        return 0;
    }
    
    • 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
    • 66
    • 67
  • 相关阅读:
    从金蝶云星空到聚水潭通过接口配置打通数据
    [附源码]计算机毕业设计疫情防控管理系统Springboot程序
    Shiro和Zuul权限管理整合方案
    树莓 LUMA-OLED.EXAMPLE使用
    git stash/git fetch/git rebase/git cherry pick/git reset
    Scratch二次开发8:背景、角色、造型、声音后台管理
    分省/市/县最低工资标准-12-2021年&1949-2020全国/省/市/县GDP数据
    Unity3D 基础——鼠标悬停更改物体颜色,移走恢复
    免费的运维监控系统PIGOSS BS基础版,欢迎下载使用
    日常问题: SQL优化
  • 原文地址:https://blog.csdn.net/weixin_39273426/article/details/127660950
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号