Notice
Recent Posts
Recent Comments
Link
알고리즘 모음(C++)
백준 10813 - 공 바꾸기(C++) 본문
문제 링크입니다. https://www.acmicpc.net/problem/10813
10813번: 공 바꾸기
도현이는 바구니를 총 N개 가지고 있고, 각각의 바구니에는 1번부터 N번까지 번호가 매겨져 있다. 바구니에는 공이 1개씩 들어있고, 처음에는 바구니에 적혀있는 번호와 같은 번호가 적힌 공이
www.acmicpc.net
배열을 이용해 푸는 문제입니다.

배열 N칸에 값이 차례대로 들어있을 때,
X, Y칸의 값을 바꾸려고 합니다.
이때는 매개변수를 하나 만들어, 값을 저장해 주고, 순서대로 바꿔주기만 하면 됩니다.
자세한 것은 코드를 참고해주세요.
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <deque>
using namespace std;
int N, M;
int arr[101];
int main() {
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for(int i = 1; i <= N; i++) arr[i] = i;
for(int i = 1; i <= M; i++){
int x, y;
cin >> x >> y;
swap(arr[x], arr[y]);
//int tmp = arr[x]; // swap 코드를 직접 작성할 때입니다.
//arr[x] = arr[y];
//arr[y] = tmp;
}
for(int i = 1; i <= N; i++) cout << arr[i] << " ";
return 0;
}

질문 및 조언은 댓글을 남겨주세요.
'백준' 카테고리의 다른 글
백준 2566 - 최댓값(C++) (0) | 2023.10.22 |
---|---|
백준 10811 - 바구니 뒤집기(C++) (0) | 2023.10.14 |
백준 10810 - 공 넣기(C++) (0) | 2023.10.13 |
백준 13903 - 출근(C++) (0) | 2023.10.10 |
백준 25418 - 정수 a를 k로 만들기(C++) (0) | 2023.10.08 |