출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
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를 쓰다보니 잘 기억이 나지 않아 헷갈렸다.
백준 1966 프린터 큐(brute force) (0) | 2020.07.18 |
---|---|
백준 7568 덩치(brute force) (0) | 2020.07.16 |
백준 2858 기숙사 바닥(brute force) (0) | 2020.07.16 |
백준 2217 로프(greedy approach) (0) | 2020.07.14 |
백준 10164 격자상의 경로(dynamic programming) (0) | 2020.07.13 |
댓글 영역