• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >编程问答 > 算法问题求解~

算法问题求解~

作者:佚名 字体:[增加 减小] 来源:互联网 时间:2017-06-07

佚名通过本文主要向大家介绍了遗传算法求解tsp问题,蚁群算法求解tsp问题,贪心算法求解背包问题,算法艺术与问题求解,遗传算法求解优化问题等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题:算法问题求解~
描述:

A big coorperation has N employees. The employees form a strict hierarchy. Each emplyee has at most one direct boss. At the same time, he or she can be in charge of many other employees as a boss. Of course, if A is B's boss and B is C's boss, A is C's boss too. In this question we only care about two properties of each employee, name and age. We define that pair ('Bill', 39) -> 'Sarah' means 39-year-old Bill is Sarah's boss. Note that Bill is also an employee and may also has a boss. Given a list of information like this, you are supposed to build the hierarchy that supports one simple query: given an employee, ouput the name of the yongest age among all his or her bosses. If the employee has no boss, output his or her own name.

The first line of input contains 2 integers P (0 < P < 1000) and Q (0 < Q < 1000). P is the number of pairs and Q is the number of queries. Each line in the next P lines contains the boss' name, age (10 < age < 100) and the employee's name. Nobody is assigned as his or her own boss. The next Q lines each contains an employee name. If the employee name being queried is not found, string "I don't know who you are talking about." should be printed. All employee names only contain upper and lower case letters.

The output should be Q lines of results in cooresponding to each query.

Sample Input:

1 3
Bill 39 Sarah
Sarah
Bill
Jayce

Sample Output:

Bill
Bill
I don't know who you are talking about.

题目大概是上面这样。。

我用C++写了一个,但是跑不起来。

大致是这样。。

typedef struct person {
char *name;
int age;
person *boss;
} person;

然后在赋值的时候建立连个数组,一个是放关系的(p_array),一个是放想要查找的名字的(q_array)。

最后在输出的时候通过两个for循环,去找是否有想对应的p_array[i].name存不存在,如果存在就判断是否有boss,有就输出他的boss,没有就输出他自己的名字。如果p_array[i].name不存在,就输出不存在。

#include <stdio.h>

typedef struct person {
    char *name;
    int age;
    struct person *boss;
} person;

int main() {
    int P;
    printf("Input Number of Paris: ");
    scanf("%d", &P);

    int Q;
    printf("Input Number of Queries: ");
    scanf("%d", &Q);

    person p_array[P];
    char *boss_name;
    int boss_age;
    char *employee_name;
    for (int i = 0; i < P; i++) {
        printf("Input the Boss's name: ");
        scanf("%s", &boss_name);
        printf("Input the Boss's age: ");
        scanf("%d", &boss_age);
        printf("Input the Employee's name: ");
        scanf("%s", &employee_name);
        p_array[i].name = employee_name;
        p_array[i].boss->name = boss_name;
        p_array[i].boss->age = boss_age;
    }

    for (int j = 0; j < P; j++) {
        printf("%s\n", p_array[j].name);
    }
    return 0;
}

解决方案1:

你的思路没有问题,跑不起来那就是你程序的问题了。
这是一个简单的树形自底向上遍历。

    typedef struct employee_t
    {
        string name;
        int age;
        employee_t* boss;
    } employee;

    map<string,employee*> employees;


分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 算法问题求解~
  • 求解算法题一枚

相关文章

  • 2017-06-07 利用python来统计一片区域内的人数
  • 2017-06-07 二级缓存和三级缓存(python)一个浏览器缓存的问题
  • 2017-06-07 按照书上的代码写,但是好像没有用到Bullet类,求解。。。
  • 2017-06-07 jboss启动速度很慢
  • 2017-06-07 Mysql更新同步到redis的问题
  • 2017-06-07 罗云彬的书第一个窗口程序的两个问题
  • 2017-06-07 (python)哪里有错误?
  • 2017-06-07 ruby脚本gets方法没有接受输入
  • 2017-06-07 免费额度超了怎么办?
  • 2017-06-07 PHP字符串中匹配url

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 一个web页面的多个相同操作模块,共用js方法。如何区分不同模块之间的操作。
    • 通过addurl远程部署程序无法成功的问题
    • 如何用wxStaticBitmap载入一个url的图片?
    • 哪里创建队列
    • javascript反向查找的正则表达式错误
    • 自己学习做的一个类论坛网站要被实际投入运作和维护,应该要学习一些什么技能?
    • 请问一下大家的iterm会这样吗,只要创建了vimrc,进入并退出vim后上面会空出来一块?
    • (python)爬虫无法输出中文
    • 在表单上传图片中用$ajax不成功
    • python多线程python两个ui线程运行

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有