본문 바로가기
연습장

Row Weights

by anothel 2021. 10. 13.

Scenario

Several people are standing in a row divided into two teams.
The first person goes into team 1, the second goes into team 2, the third goes into team 1, and so on.

 

Task

Given an array of positive integers (the weights of the people), return a new array/tuple of two integers, where the first one is the total weight of team 1, and the second one is the total weight of team 2.

 

Notes

Array size is at least 1.
All numbers will be positive.

 

Input >> Output Examples

rowWeights([13, 27, 49])  ==>  return (62, 27)

Explanation:
The first element 62 is the total weight of team 1, and the second element 27 is the total weight of team 2.

rowWeights([50, 60, 70, 80])  ==>  return (120, 140)

Explanation:
The first element 120 is the total weight of team 1, and the second element 140 is the total weight of team 2.

rowWeights([80])  ==>  return (80, 0)

Explanation:
The first element 80 is the total weight of team 1, and the second element 0 is the total weight of team 2.

 

Solution

#include <vector>
#include <utility>

using namespace std;

pair<int, int> rowWeights(const vector<int> &weights) {
  int team1(0), team2(0);
  bool bIsTeam1(true);

  for (auto x : weights) {
    if (bIsTeam1) {
      team1 += x;
    } else {
      team2 += x;
    }
    bIsTeam1 = !bIsTeam1;
  }

  return make_pair(team1, team2);
}

 

후기

벡터에 담긴 순서대로 한 번은 1팀, 한 번은 2팀으로 나눠지는데 각 팀별 체중의 합을 pair로 리턴하라는 문제이다. 사실 pair라는 자료 구조라든지, make_pair라는 stl 함수라든지 사용해본 경험이 적은 요소를 체험해볼 수 있다는 점에서 좋았고, 남들은 어떻게 짰는지 보고 싶어서 남의 코드를 봤다. 그런데 새로 만든 코드 카타여서 그런지 아니면 난이도가 낮아서 그런지 썩 맘에 드는 설루션이 없었다. 앞으로는 6 kyu 이하의 카타를 풀어보면 좋을 거 같다는 생각이 든다.

 

(url: https://www.codewars.com/kata/5abd66a5ccfd1130b30000a9)

728x90

'연습장' 카테고리의 다른 글

N과 M (1) - 15649  (0) 2021.10.18
Which are in?  (0) 2021.10.13
Simple multiplication  (0) 2021.10.13
해시 > 완주하지 못한 선수  (0) 2021.10.12
Duplicate Encoder  (0) 2021.10.12