string 비교함수 compare
#include <string>
string s;
if(s.compare("string") == 0)
cout << "same";
else
cout << "different";
compare함수는 두 문자열이 같으면 0, 다르면 -1 반환
틀렸을 때
- sqrt 함수 사용한 safety 변수를 double로 받아야 하는데 int로 했었음
- 메모리 해제를 안 했음
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string.h>
using namespace std;
int N, M;
int **seat; //N+1*M+1 배열
double cal_safety(int x, int y){ //(x,y) 좌석의 안전도 구하기
double min = 40.0;
for(int i=1; i<=N; i++){
for(int j=1; j<=M; j++){
if(seat[i][j]){
double safety = sqrt(pow(i-x, 2) + pow(j-y, 2));
if(safety < min)
min = safety;
}
}
}
return min;
}
pair<int,int> max_safety(){ //안전도가 가장 높은 좌석 구하기
double max = 0;
pair<int, int> ret = make_pair(-1, -1);
for(int i=1; i<=N; i++){
for(int j=1; j<=M; j++){
if(seat[i][j] || seat[i-1][j] || seat[i+1][j] || seat[i][j-1] || seat[i][j+1]) //배정 불가
continue;
double safety = cal_safety(i, j);
if(safety > max){
max = safety;
ret = make_pair(i, j);
}
}
}
return ret;
}
int main(int argc, char** argv)
{
int Q;
cin >> N >> M >> Q;
seat = new int*[N+2];
for(int i=0; i<N+2; i++){
seat[i] = new int[M+2];
memset(seat[i], 0, sizeof(int) * (M+2));
}
vector<int> seated;
vector<int> left;
for(int i=0; i<Q; i++){
string s;
int n;
cin >> s >> n;
if(s.compare("In") == 0){
if(find(seated.begin(), seated.end(), n) != seated.end())
cout << to_string(n) + " already seated." << endl;
else if(find(left.begin(), left.end(), n) != left.end())
cout << to_string(n) + " already ate lunch." << endl;
else if(max_safety().first == -1)
cout << "There are no more seats." << endl;
else{
if(seated.size() == 0){
seated.push_back(n);
seat[1][1] = n;
cout << to_string(n) + " gets the seat (1, 1)." << endl;
}
else{
seated.push_back(n);
pair<int,int> get = max_safety();
seat[get.first][get.second] = n;
cout << to_string(n) + " gets the seat (" + to_string(get.first) + ", " + to_string(get.second) + ")." << endl;
}
}
}
else{
if(find(left.begin(), left.end(), n) != left.end())
cout << to_string(n) + " already left seat." << endl;
else if(find(seated.begin(), seated.end(), n) == seated.end())
cout << to_string(n) + " didn't eat lunch." << endl;
else{
seated.erase(remove(seated.begin(), seated.end(), n), seated.end());
left.push_back(n);
for(int i=1; i<=N; i++)
for(int j=1; j<=M; j++)
if(seat[i][j] == n){
seat[i][j] = 0;
cout << to_string(n) + " leaves from the seat (" + to_string(i) + ", " + to_string(j) + ")." << endl;
}
}
}
}
for (int i = 0; i < N + 2; i++) {
delete[] seat[i];
}
delete[] seat;
return 0;
}
'코테준비' 카테고리의 다른 글
LCS(Longest Common Substring) 알고리즘 (1) | 2024.09.18 |
---|---|
[알고리즘] 위상정렬 (0) | 2024.09.18 |
[그래프] 백준 1389 c++ 케빈 베이컨의 6단계 법칙 (0) | 2024.03.19 |
[그래프] 백준 1260 c++ / Nested Functions, 정적배열과 동적배열, memset .. (0) | 2024.03.14 |
[브루트포스] 백준 1107 C++ / 또 무수한 틀림과 (0) | 2024.03.13 |