본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 9838 : XMAS

반응형

https://www.acmicpc.net/problem/9838

 

9838번: XMAS

Your program must write a mapping from the guests to the recipients of their gifts to the standard output. The first line contains an integer indicating the recipient of the gift brought by guest 1. Similarly, the second line contains an integer indicating

www.acmicpc.net

간단 구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

손님 수 n, 고른 선물 번호 x, .정답을 출력할 vector v를 선언한 후 입력받습니다.

📔 풀이과정

i는 1 ~ n까지 for loop를 돌며 x에 값을 입력합니다. x번 손님이 i번 선물을 고른다고 할 수 있습니다. 따라서 v[x] = i가 됩니다.

📔 정답출력

vector v의 값을 1번 방부터 형식에 맞게 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;

int n, x;
vector <int> v;

int main(){
  cin >> n;
  v.resize(n+1);
  for(int i = 1; i <= n; i++){
    cin >> x;
    v[x] = i;
  }

  for(int i = 1; i < v.size(); i++){
    cout << v[i] << '\n';
  }
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.