처음 작성한 코드는 다음과 같다.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
for(int i=0; i<t; i++) {
int n;
cin >> n;
vector<int> store;
for(int j=0; j<n; j++)
cin >> store[j];
sort(store.begin(), store.end());
int distance = 0;
for(int j=0; j<n-1; j++) {
distance += (store[j+1]-store[j]); }
distance += (store[n-1]-store[0]);
cout << distance << "\n";
}
return 0;
}
하지만 outofbounds 오류가 났다.
이 오류를 수정한 코드는 다음과 같다.
상점의 위치를 받아올 때 vector가 아니라 그냥 배열을 사용하니 해결되었다.
벡터를 처음 입력받아올 때부터 무슨 문제가 있었지 않을까 싶다.
원인은 더 찾아봐야겠다.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
for(int i=0; i<t; i++) {
int n;
cin >> n;
int store[n];
for(int j=0; j<n; j++)
cin >> store[j];
sort(store, store+n);
int distance = (store[n-1] - store[0])*2;
cout << distance << "\n";
}
return 0;
}
'백준' 카테고리의 다른 글
백준 2644 촌수계산, c++ (0) | 2024.02.23 |
---|---|
백준 16967 배열 복원하기, c++ (0) | 2024.02.22 |
백준 2023 신기한 소수, c++ (0) | 2024.02.18 |
백준 2229번: 조 짜기, c++ (0) | 2024.02.17 |
백준 1715 카드 정렬하기, c++ (0) | 2024.02.15 |