99클럽 코테 스터디 12일차 TIL + 행복

Saang Bum Kim·2024년 5월 3일
0

99클럽

목록 보기
27/59

문제

링크텍스트

난관

  • min의 초기값에 최대 가능 점수 1000점을

  • max의 초기값에 최소 가능 점수 0점을

  • 미로 주행 테스트 풀이를 위한 연습 겸해서 C++ 로도 풀어 보았다.
    - cin >> N; 이후에 getline()을 쓰면 에러가 발생한다.

    • cin 이후에 cin.ignore(); 를 써야 한다.
      • cin 은 '\n'을 남겨 놓기 때문에 getline() 이 바로 입력을 받지 않도록 해야 한다.

결과

#include<iostream>
#include <vector>
#include <string>
// #include <algorithm>

using namespace std;

int main()
{
    int N;
	string strInput;
    vector<int> scores;
    int mm[2] = {1000,0};
    int s_d;

    cin >> N;
    // cout << N << endl;
    cin.ignore();

    // getline(cin, strInput);
    // N = stoi(strInput);

    getline(cin, strInput);
    // cout << "In:" << strInput << endl;

    string strNum = "";
    for(int i = 0; i < strInput.length(); i++)
    {
        if(strInput.at(i) == ' ')
        {
            scores.push_back( atoi(strNum.c_str()) );
            strNum = "";
        }
        else
        {
            strNum += strInput.at(i);
            continue;
        }
    }
    scores.push_back( atoi(strNum.c_str()) );

    for(auto s: scores) {
 	   	mm[0] = min(mm[0],s);
	   	mm[1] = max(mm[1],s);
		s_d = mm[1]-mm[0];
	}
	cout << s_d << '\n';

    return 0;
}
N = input()
scores = map(int, input().split())

mm = [1000,0]
for s in scores:    
    mm[0] = min(mm[0],s)
    mm[1] = max(mm[1],s)
s_d = mm[1]-mm[0]
print(s_d)
    

profile
old engineer

0개의 댓글