본문 바로가기

코딩 테스트/프로그래머스 코딩테스트

최댓값과 최솟값

https://school.programmers.co.kr/learn/courses/30/lessons/12939

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>

#include <stdlib.h>
#include <cstdlib>

using namespace std;

string solution(string s) {
    string answer = "";
    
    int length = s.size();
    
    int minVal = 10000;
    int maxVal = -10000;
    
    char s1[10000];
    strcpy(s1, s.c_str());

    char *ptr = strtok(s1, " ");      // " " 공백 문자를 기준으로 문자열을 자름, 포인터 반환
    
    while (ptr != NULL)               // 자른 문자열이 나오지 않을 때까지 반복
    {
        int number = atoi(ptr);
        
        // Find Min Max Value
        if(maxVal < number)
            maxVal = number;
        if(minVal > number)
            minVal = number;
        
        ptr = strtok(NULL, " ");      // 다음 문자열을 잘라서 포인터를 반환
             
    }
    
//    printf("최대 값 = %d\n", maxVal);
//    printf("최소 값 = %d\n", minVal);
    
    answer += std::to_string(minVal);
    answer += " ";
    answer += std::to_string(maxVal);
    
   printf("정답 = %s\n", answer.c_str());
    
    return answer;
}

'코딩 테스트 > 프로그래머스 코딩테스트' 카테고리의 다른 글

올바른 괄호  (0) 2022.10.03
신규 아이디 추천 - 미해결...  (0) 2022.10.03
행렬의 곱셈  (0) 2022.09.29
성격 유형 검사하기  (0) 2022.09.25
숫자 문자열과 영단어 풀이  (0) 2022.09.25