출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]

상세 컨텐츠

본문 제목

백준 4641 Doubles(brute force)

알고리즘

by 장동균 2020. 7. 16. 23:19

본문

https://www.acmicpc.net/problem/4641

 

4641번: Doubles

문제 2~15개의 서로 다른 자연수로 이루어진 리스트가 있을 때, 이들 중 리스트 안에 자신의 정확히 2배인 수가 있는 수의 개수를 구하여라. 예를 들어, 리스트가 "1 4 3 2 9 7 18 22"라면 2가 1의 2배, 4�

www.acmicpc.net

 

#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX = 15 + 1;
int arr[MAX];
vector<int> v;
int countNum;

void doubleArray()
{
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)
    {
        for (int j = i + 1; j < v.size(); j++)
        {
            if (v[i] * 2 == v[j])
                countNum++;
        }
    }
    cout << countNum << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    while (1)
    {
        int checkEnd;
        cin >> checkEnd;
        if (checkEnd == -1)
            break;
        v.push_back(checkEnd);
        while (1)
        {
            int tmp;
            cin >> tmp;
            if (tmp == 0)
                break;
            v.push_back(tmp);
        }
        doubleArray();
        v.clear();
        countNum = 0;
    }
}

크게 이해하기 어려운 부분을 없었다. 다만 너무 오랜만에 vector를 쓰다보니 잘 기억이 나지 않아 헷갈렸다.

관련글 더보기

댓글 영역