본문 바로가기
Algorithm/C++

[프로그래머스 C++] 문자열 내림차순으로 배치하기

by imagineer_jinny 2021. 8. 30.

코딩테스트 연습 - 문자열 내림차순으로 배치하기 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 문자열 내림차순으로 배치하기

문자열 s에 나타나는 문자를 큰것부터 작은 순으로 정렬해 새로운 문자열을 리턴하는 함수, solution을 완성해주세요. s는 영문 대소문자로만 구성되어 있으며, 대문자는 소문자보다 작은 것으로

programmers.co.kr

 

문제 설명

문자열 s에 나타나는 문자를 큰것부터 작은 순으로 정렬해 새로운 문자열을 리턴하는 함수, solution을 완성해주세요.
s는 영문 대소문자로만 구성되어 있으며, 대문자는 소문자보다 작은 것으로 간주합니다.

제한 사항

  • str은 길이 1 이상인 문자열입니다.

 

입출력 예

s return
"Zbcdefg" "gfedcbZ"

 


풀이1

#include <iostream>

using namespace std;
int solution(int n)
{
    int answer = 0;

   
  while(n!=0)
  {
      answer+=n%10;
      n/=10;
  }
    
    

    return answer;
}

 

풀이2

#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

string solution(string s) {
    sort (s.begin(), s.end(), greater<char>());
    return s;
}

댓글