웅재의 코딩세상
[프로그래머스] 수학, 시뮬레이션, 문자열, 사칙연산 본문
- 직사각형 넓이 구하기
#include <string>
#include <vector>
using namespace std;
int solution(vector<vector<int>> dots) {
int x_min = min(min(dots[0][0],dots[1][0]),min(dots[2][0],dots[3][0]));
int x_max = max(max(dots[0][0],dots[1][0]),max(dots[2][0],dots[3][0]));
int y_min = min(min(dots[0][1],dots[1][1]),min(dots[2][1],dots[3][1]));
int y_max = max(max(dots[0][1],dots[1][1]),max(dots[2][1],dots[3][1]));
return (x_max-x_min)*(y_max-y_min);
}
- 캐릭터의 좌표
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<string> keyinput, vector<int> board) {
vector<int> answer;
int col=0, row=0;
if(keyinput.size()==0){
answer.push_back(row);
answer.push_back(col);
return answer;
}
for(int i=0; i< keyinput.size(); i++){
if(keyinput[i]=="left") row--;
else if(keyinput[i]=="right") row++;
else if(keyinput[i]=="up") col++;
else col--;
if((board[0] -1)/2 < abs(row)) {
if(row < 0) row = (-1)*(board[0] -1)/2;
else row = (board[0] -1)/2;
}
if((board[1] -1)/2 < abs(col)) {
if(col < 0) col = (-1)*(board[1] -1)/2;
else col = (board[1] -1)/2;
}
}
answer.push_back(row);
answer.push_back(col);
return answer;
}
- 최댓값 만들기(2)
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> numbers) {
int answer;
int pos;
int neg;
sort(numbers.begin(), numbers.end(), greater<int>());
pos = numbers[0]*numbers[1];
sort(numbers.begin(), numbers.end());
neg = numbers[0]*numbers[1];
return pos > neg ? pos : neg;
}
- 다항식 더하기
#include <string>
#include <vector>
#include <sstream>
using namespace std;
string solution(string polynomial) {
string answer;
stringstream ss(polynomial);
int x_sum=0, n_sum=0;
while(getline(ss,answer,' ')){
if(answer.back() == 'x'){
if(answer.size() == 1) x_sum++;
else x_sum += stoi(string(answer.begin(),answer.end()-1));
}
else if(answer != "+") n_sum += stoi(answer);
}
if(x_sum==0) return to_string(n_sum);
else{
string s;
if(x_sum == 1) s = "x";
else s = to_string(x_sum) + "x";
if(n_sum==0) return s;
else return s + " + " + to_string(n_sum);
}
}
반응형
'코딩테스트 > 프로그래머스 - LV 0' 카테고리의 다른 글
[프로그래머스] dp, 수학, 조건문, 배열 (0) | 2024.01.19 |
---|---|
[프로그래머스] 문자열, 사칙연산, 시뮬레이션, 2차원 배열 (0) | 2024.01.19 |
[프로그래머스] 문자열, 배열, 조건문 (1) | 2024.01.13 |
[프로그래머스] 문자열, 수학, 조건문, 정렬 (1) | 2024.01.13 |
[프로그래머스] 문자열, 수학, 조건문, 배열, 사칙연산 (0) | 2024.01.12 |