-
[백준]2064 - IP 주소 (구현)코테/백준 2023. 6. 27. 00:06
https://www.acmicpc.net/problem/2064
2064번: IP 주소
네트워크에 연결되어 있는 컴퓨터들은 각각 하나의 IP 주소를 갖게 된다. 그리고 이러한 IP 주소를 갖는 컴퓨터들이 여러 개 모여서 하나의 IP 네트워크를 구성하게 된다. IP 네트워크는 ‘네트워
www.acmicpc.net
IP 주소의 개수와 각 주소값이 주어질때
네트워크 주소와 마스크 값을 구하여라
(단 네트워크의 크기는 최대한 작은 크기여야한다)
int main() { int nn; cin >> nn; vector<string> s(nn); for (int i = 0; i < nn; i++) { cin >> s[i]; }
IP주소 입력받기
vector<string> split(string input, char dlim) { vector<string> result; stringstream ss; string stringBuffer; ss.str(input); while (getline(ss, stringBuffer, dlim)) { result.push_back(stringBuffer); } return result; }
vector<vector<int>>v(nn); for (int i = 0; i < s.size(); i++) { vector<string> result = split(s[i], '.'); for (int j = 0; j < result.size(); j++) { int t = stoi(result[j]); v[i].push_back(t); } }
입력받은 주소를 . 기준으로 스플릿하여 2차원 벡터에 INT로 저장
if (v.size() == 1){ cout << s[0]<<"\n"<<"255.255.255.255"; return 0; }
입력받은 주소가 1개일 경우 예외처리
sort(v.begin(), v.end()); int vv = v.size() - 1; int a[32]; int b[32]; for (int i = 0; i < 4; i++) { int aa = 128; int bb = 128; for (int j = 0; j < 8; j++) { if (v[0][i] >= aa) { a[j + i * 8] = 1; v[0][i] -= aa; } else { a[j + i * 8] = 0; } aa /= 2; if (v[vv][i] >= bb) { b[j + i * 8] = 1; v[vv][i] -= bb; } else { b[j + i * 8] = 0; } bb /= 2; } } int index=32; for (int i = 0; i < 32; i++) { if (a[i] != b[i]) { index = i; break; } }
네트워크의 크기 구하기
주어진 IP 주소들을 정렬하여
가장 큰 값과 작은 값을 구하고
각각의 주소값을 2진수로 변환해 a[32], b[32]에 저장
두 배열을 비교하여 값이 달라지는 첫번째 인덱스를 구한다
int m[32]; for (int i = index; i < 32; i++) { m[i] = 0; } for (int i = 0; i < index; i++) { m[i] = 1; }
해당 인덱스만큼 1, 그 이후엔 0으로 구성된 배열을 만든다
이를 다시 10진수로 변환하면 서브넷마스크.
int n[32]; for (int i = 0; i < 32; i++) { if (m[i] == 1) { n[i] = a[i]; } else { n[i] = 0; } }
서브넷마스크값이 1이면 가장 작은 주소값을, 아니면 0을 리턴한다
이것이 주소의 최소값(시작값)이다
int mt[4]; int nt[4]; for (int i = 0; i < 4; i++) { int k = 0; int kk = 0; int t = 128; for (int j = 0; j < 8; j++) { if (m[j + i * 8] == 1) { k += t; } if (n[j + i * 8] == 1) { kk += t; } t /= 2; } mt[i] = k; nt[i] = kk; }
두 배열은 10진수로 변환하고
for (int i = 0; i < 4; i++) { cout << nt[i]; if (i != 3) cout << "."; } cout << "\n"; for (int i = 0; i < 4; i++) { cout << mt[i]; if (i != 3) cout << "."; }
.과 함께 출력하면 문제 해결
구현 자체는 쉽지만
이를 위해 문제를 이해하는게 어려웠던 것 같다.
전체 코드
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <sstream> using namespace std; vector<string> split(string input, char dlim) { vector<string> result; stringstream ss; string stringBuffer; ss.str(input); while (getline(ss, stringBuffer, dlim)) { result.push_back(stringBuffer); } return result; } int main() { int nn; cin >> nn; vector<string> s(nn); for (int i = 0; i < nn; i++) { cin >> s[i]; } vector<vector<int>>v(nn); for (int i = 0; i < s.size(); i++) { vector<string> result = split(s[i], '.'); for (int j = 0; j < result.size(); j++) { int t = stoi(result[j]); v[i].push_back(t); } } if (v.size() == 1){ cout << s[0]<<"\n"<<"255.255.255.255"; return 0; } sort(v.begin(), v.end()); int vv = v.size() - 1; int a[32]; int b[32]; for (int i = 0; i < 4; i++) { int aa = 128; int bb = 128; for (int j = 0; j < 8; j++) { if (v[0][i] >= aa) { a[j + i * 8] = 1; v[0][i] -= aa; } else { a[j + i * 8] = 0; } aa /= 2; if (v[vv][i] >= bb) { b[j + i * 8] = 1; v[vv][i] -= bb; } else { b[j + i * 8] = 0; } bb /= 2; } } int index=32; for (int i = 0; i < 32; i++) { if (a[i] != b[i]) { index = i; break; } } int m[32]; for (int i = index; i < 32; i++) { m[i] = 0; } for (int i = 0; i < index; i++) { m[i] = 1; } int n[32]; for (int i = 0; i < 32; i++) { if (m[i] == 1) { n[i] = a[i]; } else { n[i] = 0; } } int mt[4]; int nt[4]; for (int i = 0; i < 4; i++) { int k = 0; int kk = 0; int t = 128; for (int j = 0; j < 8; j++) { if (m[j + i * 8] == 1) { k += t; } if (n[j + i * 8] == 1) { kk += t; } t /= 2; } mt[i] = k; nt[i] = kk; } for (int i = 0; i < 4; i++) { cout << nt[i]; if (i != 3) cout << "."; } cout << "\n"; for (int i = 0; i < 4; i++) { cout << mt[i]; if (i != 3) cout << "."; } return 0; }
'코테 > 백준' 카테고리의 다른 글
[백준]1260 - DFS와 BFS (0) 2024.02.22 [백준]22345 - 누적거리 (누적합) (0) 2023.06.26 [백준]17825 - 주사위 윷놀이 (구현, DFS, 백트래킹) (0) 2023.06.25 [백준]24467 - 혼자하는 윷놀이 (0) 2023.06.24 [백준]2252 - 줄세우기 (DFS + 위상정렬) (0) 2023.06.22