×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

我瞎编的答案。怎么到处都是重复代码?好像不对劲啊 😁😁有没有C++高手来帮着批改一下?先谢谢

struct Problem;

struct Solution;

class Skill {

public:

Skill() = default;

Solution Solve(Problem problem);

}

// Implement class to represent an engineer. Each engineer could have multiple skills.

// Implement class to represent a team. Each team could have multiple engineers.

// A team class needs to provide a Solve function: given a problem, whether it could be solved

// and what is the solution. A problem could be solved if there is an engineer in the team that

// could provide a solution to it.

// My solution:

class Engineer{

public:

Engineer() = default;

void setSkill(const Skill& skill);

Solution Solve(Problem problem) const;

private:

std::set<Skill> skills;

};

Solution Engineer::Slove(Problem problem) const

{

if(skills.empty())

return Solution::NOSOLUTION;

for(auto i : skills{

Solution solution = i.solve(problem);

if (solution != Solution::NOSOLUTION)

return solution;

}

return Solution::NOSOLUTION;

}

Solution Engineer::setSkill(const Skill& skill){

skills.insert(skill);

}

class Team{

public:

Team() = default;

void addMember(const Engineer&);

SolutionSolve(Problem problem) const;

private:

std::set<Engineer> engineers;

}

void Team::addMember(const Engineer& engeineer){

engineers.insert(engeineer);

}

Solution Team::Slove(Problem problem) const

{

if(engineers.empty())

return Solution::NOSOLUTION;

for(auto i : engineers{

Solution solution = i.solve(problem);

if (solution != Solution::NOSOLUTION)

return solution;

}

return Solution::NOSOLUTION;

}



Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术 / 请教一下,这道题咋做?用C++。先谢谢

    struct Problem;

    struct Solution;

    class Skill {

    public:

    Skill() = default;

    Solution Solve(Problem problem);

    }

    // Implement class to represent an engineer. Each engineer could have multiple skills.

    // Implement class to represent a team. Each team could have multiple engineers.

    // A team class needs to provide a Solve function: given a problem, whether it could be solved

    // and what is the solution. A problem could be solved if there is an engineer in the team that

    // could provide a solution to it.

    • 不用谢 +3
      不会做
      • 非常感谢。。。😁 +1
        跟帖😁
    • 这个不给40万,天理难容。
      • 会做就帮个忙,说不定能混个20万的工作😁😁
    • Implement “超人变身”
    • 是谁要做?这都要问人,那就放弃算了……
      • 谁要做不重要。学习嘛 +1
        • 就算是学习,这种题目也要问人的话,学习的目的是? +2
          • 从零基础学起。。。不懂就问呗。还能有啥别的目的?😂😂😁
            • 那我说句不好听的,要么从更基础的开始,要么就别学了……
              • 谢谢忠告。我还就愿意学这道题😁😁 +1
    • 为啥要学c++
      • 吃饱了撑的😁😁我跟人家吹我会,总得多会一点嘛。
        • 会这一点也没法吹
          • 吹一点,是一点,慢慢吹,啊不学嘛😁
            • 吹点别的吧😁
              • 别的当然也吹了😂😂🤣🤣
    • 我迷惑的一点,主要在于怎么知道一个Skill对一个Problem,是不是有Solution涅?要在Solution定义里面做枚举吗?或者定义null就是没有solution?
      • 这个一般是problem 有个ID,然后到数据库查找,找到对应的Solution ID,用ID再生成对应的Solution Object.
        • 谢谢。听起来像是工厂模式😁😁
          • 你 overthinking 了。这是道 $25 时薪的题,题里问什么,你就答什么。
            • 确实,我把它想复杂了,其实非常简单😂😂行了,现在我有信心拿到25元时薪了!欧耶✌️😂😂
    • 谁出的题?拉黑。 +1
      • 人家把我拉黑了🤣🤣 +1
    • 我瞎编的答案。怎么到处都是重复代码?好像不对劲啊 😁😁有没有C++高手来帮着批改一下?先谢谢

      struct Problem;

      struct Solution;

      class Skill {

      public:

      Skill() = default;

      Solution Solve(Problem problem);

      }

      // Implement class to represent an engineer. Each engineer could have multiple skills.

      // Implement class to represent a team. Each team could have multiple engineers.

      // A team class needs to provide a Solve function: given a problem, whether it could be solved

      // and what is the solution. A problem could be solved if there is an engineer in the team that

      // could provide a solution to it.

      // My solution:

      class Engineer{

      public:

      Engineer() = default;

      void setSkill(const Skill& skill);

      Solution Solve(Problem problem) const;

      private:

      std::set<Skill> skills;

      };

      Solution Engineer::Slove(Problem problem) const

      {

      if(skills.empty())

      return Solution::NOSOLUTION;

      for(auto i : skills{

      Solution solution = i.solve(problem);

      if (solution != Solution::NOSOLUTION)

      return solution;

      }

      return Solution::NOSOLUTION;

      }

      Solution Engineer::setSkill(const Skill& skill){

      skills.insert(skill);

      }

      class Team{

      public:

      Team() = default;

      void addMember(const Engineer&);

      SolutionSolve(Problem problem) const;

      private:

      std::set<Engineer> engineers;

      }

      void Team::addMember(const Engineer& engeineer){

      engineers.insert(engeineer);

      }

      Solution Team::Slove(Problem problem) const

      {

      if(engineers.empty())

      return Solution::NOSOLUTION;

      for(auto i : engineers{

      Solution solution = i.solve(problem);

      if (solution != Solution::NOSOLUTION)

      return solution;

      }

      return Solution::NOSOLUTION;

      }



      • 一对小改动

        class Skill 的 Solution Solve(Problem problem); 最后加个 const

        class Engineer::Solve 里面的 for loop 就可以改成 for (const auto& i : skills)

        • 谢谢老大😁👍
    • 这些是class 的inherit 基本定义吧。 二十多年没有OO编程的路过胡说一句
      • 我觉得这里面的类,都不是继承关系,是has a。composition或者aggregration,叫啥来着,组合或者聚合关系吧?
    • 可以用 github copilot 产生 c#
      public struct Problem { }
      public struct Solution { }
      public class Skill
      {
          public Solution? Solve(Problem problem) 
          { throw new NotImplementedException();  }
      }
      
      // define a class to represent an engineer. Each engineer could have multiple skills.
      public class Engineer
      {
          public string Name { get; set; }
          public List<Skill> Skills { get; set; }
      }
      
      // define a class to represent a team. Each team could have multiple engineers.
      public class Team
      {
          public string Name { get; set; }
          public List<Engineer> Engineers { get; set; }
          public Solution? Solve(Problem problem)
          {
              foreach (var engineer in Engineers)
              foreach (var skill in engineer.Skills)
              {
                  var solution = skill.Solve(problem);
                  if (solution != null) return solution;
              }
      
              return null;
          }
      }
      
      • 谢谢
        • 这个是 copilot 自动产生的 c++ code...
          struct Problem;
          
          struct Solution;
          
          
          class Skill {
          
          public:
              Skill() = default;
              Solution Solve(Problem problem);
          }
          
          
          // define a class to represent an engineer. Each engineer could have multiple skills.
          class Engineer {
          public:
              Engineer() = default;
              Engineer(std::string name, std::vector<Skill> skills) : name_(name), skills_(skills) {}
              std::string name_;
              std::vector<Skill> skills_;
          
              // solve a problem using the engineer's skills
              Solution Solve(Problem problem) {
                  for (auto skill : skills_) {
                      Solution solution = skill.Solve(problem);
                      if (solution) {
                          return solution;
                      }
                  }
                  return Solution();
              }
          }
          
          // define a class to represent a team. Each team could have multiple engineers.
          class Team {
          public:
              Team() = default;
              Team(std::string name, std::vector<Engineer> engineers) : name_(name), engineers_(engineers) {}
              std::string name_;
              std::vector<Engineer> engineers_;
          
              // solve a problem using the team's engineers
              Solution Solve(Problem problem) {
                  for (auto engineer : engineers_) {
                      Solution solution = engineer.Solve(problem);
                      if (solution) {
                          return solution;
                      }
                  }
          
                  return Solution();
              }
          }
          • 这不跟我遍的差不多吗?他用vector,我用set,我还消除了冗余
            • 我只敲了 // 注释,剩下的活儿都是 copilot 干的...
              • 谢谢。这东西不错。
    • 难点在哪里?比如是懂得怎么写oo style的代码,用Java能写但是C++写不出来吗?还是用啥语言都不会。这题的要求还挺基本的,我要是用c++写可能得查一下语法因为忘得差不多了
      • 没啥难点,我不是不熟嘛。Java我更不会了😁😁我在上面编了一下,有空帮看看
        • 哈哈写了一半放弃了。C++语法忘得太多,特别是std那些库.
          • 怎么样?老大,不容易吧?主要不总玩
            • 是,不敢吹牛了😅
    • 顺便温习STL, 写driver多了, 人都傻了 (网站贴代码功能咋用的?)
      
      
      #include gtest plz
      
      class Skill {
          public:
              enum class SID { NONE,
                               EAT, SLEEP, BEAT_DOUDOU,
                               LADY_READ /*hard skill, contact you@are.lying.com if feel confident*/ };
      
              Skill(SID sid) : m_sid(sid) {};
      
          private:
              const SID m_sid = SID::NONE;
      };
      
      struct Solution {
      public:
          Solution(bool solved) : m_solved(solved) { };
          Solution(const Solution& others) = default;
          Solution& operator=(const Solution& others) = default;
      
          bool Solved() const { return m_solved; }
      
      private:
          bool m_solved { false };
      };
      
      class Problem {
      public:
          Problem() = delete;
          Problem(std::initializer_listconst & skills): m_desiredSkills(skills) { }
      
          struct WorkTable {
              WorkTable(size_t size) : m_result(size, false) {}
              bool Done() const { return std::find(begin(m_result), end(m_result), false) == end(m_result); }
              void SetSolved(size_t index){ m_result[index] = true; }
              vector m_result;
          };
      
          WorkTable NewWorkTable() const { return WorkTable(m_mustHaveSkills.size()); }
      
          void Research(const Skill::SID sid, WorkTable& worktable) const {
              auto itor = m_mustHaveSkills.find(sid);
              if(itor != m_mustHaveSkills.end()) {
                  worktable.SetSolved(abs(std::distance(m_mustHaveSkills.begin(), itor)));
              }
          }
      
          void Research(const std::unordered_set& skills, WorkTable& worktable) const {
              for(auto sid : skills) {
                  Research(sid, worktable);
              }
          }
      
      private:
          std::unordered_set m_mustHaveSkills;
      };
      
      class Engineer {
          public:
              Engineer() = delete; // no skill? not a engineer.
              Engineer(std::initializer_listconst & skills) : m_skills(skills) { };
      
              void Work(const Problem& problem, Problem::WorkTable& worktable) const {
                  problem.Research(m_skills, worktable);
              }
      
          private:
              std::unordered_set m_skills;
      };
      
      class Team {
          public:
              Team() = delete;
              Team(std::initializer_listconst & engineers) : m_engineers(engineers) { };
      
              Solution Work(const Problem& problem) const {
                  auto worktable = problem.NewWorkTable();
                  for(auto& e : m_engineers) {
                      e.Work(problem, worktable);
                      if(worktable.Done()) {
                          return Solution(true);
                      }
                  }
                  return Solution(false);
              }
      private:
          std::vector m_engineers;
      };
      
      TEST(Q0007_SIMPLE, TEAM1)
      {
          Team team_1 {
              { {Skill::SID::EAT} },
              { {Skill::SID::SLEEP } },
              { {Skill::SID::EAT, Skill::SID::SLEEP } },
              { {Skill::SID::BEAT_DOUDOU, Skill::SID::SLEEP } },
          };
      
          Problem p1 { Skill::SID::BEAT_DOUDOU };
          EXPECT_EQ(team_1.Work(p1).Solved(), true );
      
          Problem p2 { Skill::SID::LADY_READ };
          EXPECT_EQ(team_1.Work(p2).Solved(), false );
      }