GitHubSeob
C++ / 프로그래머스 / 최댓값과 최솟값 본문
문제
https://school.programmers.co.kr/learn/courses/30/lessons/12939
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제풀이
문자열을 파싱 하는 istringstream을 이용해 풀었다.
istr에서 공백 (' ')을 기준으로 문자열을 잘라 num에 저장한다.
min_num에는 최솟값을, max_num에는 최댓값을 저장한다.
문제에 숫자 범위 조건이 안 나와있어 min_num에는 int의 최댓값인 2147483647, max_num에는 최솟값인 -2147483648으로 초기화했다.
마지막으로 answer에 최솟값 + " " + 최댓값을 저장하고 return 한다.
코드
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#define MAX 2147483647
#define MIN -2147483648
using namespace std;
string solution(string s) {
string answer(""), num("");
int min_num(MAX), max_num(MIN);
istringstream istr(s);
while (getline(istr, num, ' ')) {
min_num = min(stoi(num), min_num);
max_num = max(stoi(num), max_num);
}
answer += to_string(min_num);
answer += " ";
answer += to_string(max_num);
return answer;
}
'Programmers > Level 2' 카테고리의 다른 글
C++ / 프로그래머스 / 올바른 괄호 (0) | 2023.06.26 |
---|---|
C++ / 프로그래머스 / JadenCase 문자열 만들기 (0) | 2023.06.22 |
C++ / 프로그래머스 / 피로도 (0) | 2022.04.23 |
C++ / 프로그래머스 / 큰 수 만들기 (0) | 2022.04.23 |
C++ / 프로그래머스 / 카펫 (0) | 2022.04.23 |