-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12931.cpp
75 lines (70 loc) · 1.34 KB
/
12931.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 12931. 두 배 더하기
// 2020.04.23
// 구현
#include<iostream>
#include<vector>
using namespace std;
int n;
// 모두 2의배수인지 체크하는 함수
bool check(vector<int>& v)
{
for (int j = 0; j < n; j++)
{
if (v[j] % 2 == 1)
{
return false;
}
}
return true;
}
// 모두 0인지 체크하는 함수
bool allZero(vector<int>& v)
{
for (int j = 0; j < n; j++)
{
if (v[j] != 0)
{
return false;
}
}
return true;
}
int main()
{
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
int ans = 0;
// A->B가 아닌 B->A를 계산한다.
while (1)
{
// 하나라도 2의 배수가 아니라면 돌면서 2의 배수가 아닌것 -1하고 연산횟수 +1
for (int i = 0; i < n; i++)
{
if (v[i] % 2 == 1)
{
v[i]--;
ans++;
}
}
// 모두 0이면 종료
if (allZero(v))
{
break;
}
// 모두 2의 배수라면 2로 나누고 연산횟수 +1
if (check(v))
{
for (int j = 0; j < n; j++)
{
v[j] /= 2;
}
ans++;
}
}
cout << ans << endl;
return 0;
}