알고리즘 문제
[백준] 4641번 Doubles
feelcoding
2020. 2. 11. 04:42
728x90
https://www.acmicpc.net/problem/4641
4641번: Doubles
문제 2~15개의 서로 다른 자연수로 이루어진 리스트가 있을 때, 이들 중 리스트 안에 자신의 정확히 2배인 수가 있는 수의 개수를 구하여라. 예를 들어, 리스트가 "1 4 3 2 9 7 18 22"라면 2가 1의 2배, 4가 2의 2배, 18이 9의 2배이므로 답은 3이다. 입력 입력은 여러 개의 테스트 케이스로 주어져 있으며, 입력의 끝에는 -1이 하나 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있으며, 2~15개의 서로 다른 자연수가 주어진다.
www.acmicpc.net
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
int main() {
while (true) {
int first;
cin >> first;
if (first == -1) break;
vector<int> v;
v.push_back(first);
while (true) {
int n;
cin >> n;
if (n == 0) break;
v.push_back(n);
}
int count = 0;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v.size(); j++) {
if (v[j] == v[i] * 2) count++;
}
}
cout << count << '\n';
}
return 0;
}
728x90