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

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>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;
}

华为机试C语言-找到比自己强的人数相关推荐

  1. 华为机试C语言-最远足迹

    题目描述:https://pycoder.blog.csdn.net/article/details/125531902 https://blog.csdn.net/weixin_44052055/a ...

  2. 华为机试C语言-服务失效判断

    题目描述:https://zhuanlan.zhihu.com/p/526678876 https://blog.csdn.net/weixin_39765576/article/details/12 ...

  3. 华为机试C语言-VLAN资源池

    题目描述:https://pycoder.blog.csdn.net/article/details/125175834 #include <stdio.h> #include <s ...

  4. 华为机试C语言-整形数组按个位值排序

    题目描述:https://pycoder.blog.csdn.net/article/details/124600540 #include <stdio.h> #include <s ...

  5. 华为机试C语言-组成最大数

    题目描述:https://pycoder.blog.csdn.net/article/details/125923027 想不到这也是一道qsort的题目~ #include <stdio.h& ...

  6. 华为机试C语言-Words重量

    题目描述:https://pycoder.blog.csdn.net/article/details/124743820 #include <stdio.h> #include <s ...

  7. 华为机试C语言-篮球比赛

    题目描述:https://pycoder.blog.csdn.net/article/details/125270003?spm=1001.2014.3001.5502 动态规划 : dp[i] 表示 ...

  8. 华为机试C语言-一个正整数到Excel编号之间的转换

    题目描述:https://blog.csdn.net/qq_23934063/article/details/124676929 #include <stdio.h> #include & ...

  9. 华为机试C语言-欢乐的周末

    题目描述:https://pycoder.blog.csdn.net/article/details/125401136 递归,回溯的时候要考虑周全,不然白理解了. #include <stdi ...

最新文章

  1. 习题8-5 使用函数实现字符串部分复制 (20 分)
  2. python解除windows锁屏_实战 | Python批量提取Win10锁屏壁纸
  3. 7-6 混合类型数据格式化输入 (C语言)
  4. imx6 休眠 功耗 电流_无线物联网和可穿戴设备的低功耗电源测量挑战
  5. as3.0-----计时器的使用实例
  6. spring boot 带远程调试启动方式
  7. php判断全是中文正则,php判断是否为中文正则表达式大全
  8. vue 父组件获取接口值传到子组件_vue父组件异步获取数据传给子组件的方法
  9. 《深入Ajax架构和最佳实践》读书笔记
  10. 涉及到整型参数时,没有初始化时,Debug下可以运行,但是Release下不行
  11. 快应用采坑与flex布局讲解
  12. 解决Mysql数据库提示innodb表不存在的问题
  13. 比赛打分系统的计时器数字或滚动抽签数字不显示的原因解惑
  14. 服务器显示RL011,台达伺服驱动器维修之AL011故障原因和方法
  15. 小程序 富文本WxParse 内容循环
  16. XJad(Java反编译工具)v2.2绿色版
  17. 马哥linux架构班笔记,51CTO博客-专业IT技术博客创作平台-技术成就梦想
  18. html中字体 楷体_(收藏)css怎么设置字体为楷体?
  19. 电路串联和并联图解_判断串联并联电路图口诀
  20. python3连接到sql server数据库,检查是否存在某个数据库,没有则创建

热门文章

  1. opencv PIL读取图像得到的图像格式
  2. vue2和elementUI 打造落日余晖登录页和滑块校验
  3. app 与服务端通讯方式概览
  4. 继小米机器狗、特斯拉机器人后,小鹏汽车发布智能机器马:真的能骑!
  5. 金蝶旗舰版当期新增固定资产,当期就计提折旧
  6. 开发一个商城小程序要多少钱
  7. 自制USB充电线、数据线、工程线、OTG线的方法
  8. adf盖怎么打开_兄弟7470d显示ADF盖打开是什么问?
  9. Java最牛教材!javaexcel合并单元格样式
  10. 基于Python实现Jacobi迭代法、Gauss-Seidel迭代法和SOR法方程组(三)