문제 설명
함수 solution은 정수 n을 매개변수로 입력받습니다. n의 각 자릿수를 큰것부터 작은 순으로 정렬한 새로운 정수를 리턴해주세요. 예를들어 n이 118372면 873211을 리턴하면 됩니다.
제한 조건
n은 1이상 8000000000 이하인 자연수입니다.
입출력 예
n return
118372 873211
#include <algorithm>
using namespace std;
long long solution(long long n) {
long long answer = 0;
string str = to_string(n);
sort(str.begin(), str.end(), greater<char>());
//sort(str.begin(), str.end(), less<char>());
answer = stoll(str);
return answer;
}
string str = to_string(n);
long long answer = stoll(str);
int answer = stoi(s);
sort(str.begin(), str.end(), greater<char>());
sort(str.begin(), str.end(), less<char>());
reverse(str.begin(), str.end());
문제 설명
문자열 my_string이 매개변수로 주어집니다. my_string을 거꾸로 뒤집은 문자열을 return하도록 solution 함수를 완성해주세요.
제한사항
1 ≤ my_string의 길이 ≤ 1,000
입출력 예
my_string return
"jaron" "noraj"
"bread" "daerb"
#include <string>
#include <vector>
using namespace std;
string solution(string my_string) {
string answer = "";
for(int i = my_string.size() - 1; i >= 0; i--)
answer += my_string[i];
return answer;
}
for(int i = my_string.size() - 1; i >= 0; i--)
#include <algorithm>
using namespace std;
string solution(string my_string) {
reverse(my_string.begin(),my_string.end());
return my_string;
}
문제 설명
문자열 my_string과 문자 letter이 매개변수로 주어집니다. my_string에서 letter를 제거한 문자열을 return하도록 solution 함수를 완성해주세요.
제한사항
1 ≤ my_string의 길이 ≤ 100
letter은 길이가 1인 영문자입니다.
my_string과 letter은 알파벳 대소문자로 이루어져 있습니다.
대문자와 소문자를 구분합니다.
입출력 예
my_string letter result
"abcdef" "f" "abcde"
"BCBdbe" "B" "Cdbe"
#include <algorithm>
using namespace std;
string solution(string my_string, string letter) {
my_string.erase(remove(my_string.begin(),my_string.end(),letter[0]),my_string.end());
return my_string;
}
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string my_string) {
vector<char> v = {'a', 'e', 'i', 'o', 'u'};
for(auto i : v)
{
my_string.erase(remove(my_string.begin(),my_string.end(),i ),my_string.end());
}
return my_string;
}
remove(v.begin(), v.end(), ' ')
erase(v.begin(), v.end())
string 변수도 for문을 통해 한개씩 분해할 수 있다.
#include <string>
using namespace std;
string solution(string my_string, string letter) {
string answer = "";
for (auto c : my_string) {
if (c != letter) {
answer += c;
}
}
return answer;
}
1-1. 입력된 수가 짝수라면 2로 나눕니다.
1-2. 입력된 수가 홀수라면 3을 곱하고 1을 더합니다.
2. 결과로 나온 수에 같은 작업을 1이 될 때까지 반복합니다.
예를 들어, 주어진 수가 6이라면 6 → 3 → 10 → 5 → 16 → 8 → 4 → 2 → 1 이 되어 총 8번 만에 1이 됩니다. 위 작업을 몇 번이나 반복해야 하는지 반환하는 함수, solution을 완성해 주세요. 단, 주어진 수가 1인 경우에는 0을, 작업을 500번 반복할 때까지 1이 되지 않는다면 –1을 반환해 주세요.
#include <string>
#include <vector>
using namespace std;
int solution(int num) {
int answer = 0;
long long n = num;
while(n>1)
{
n%2==0 ? n/=2 : n=n*3+1;
answer++;
if(answer>=500) return -1;
}
return answer;
}
"num은 1 이상 8,000,000 미만인 정수입니다."라는 조건 때문에 'n=n3+1'를 적용하면 int한계를 넘어가는 숫자가 될 수 있기 때문에 long long을 사용했다. 왜 안될까를 오래 생각했는데 처음 num이 큰숫자에 3을 하면 한계를 초과할 거란 생각을 하기가 어려웠다.
문제 설명
양의 정수 x가 하샤드 수이려면 x의 자릿수의 합으로 x가 나누어져야 합니다. 예를 들어 18의 자릿수 합은 1+8=9이고, 18은 9로 나누어 떨어지므로 18은 하샤드 수입니다. 자연수 x를 입력받아 x가 하샤드 수인지 아닌지 검사하는 함수, solution을 완성해주세요.
제한 조건
x는 1 이상, 10000 이하인 정수입니다.
입출력 예
x return
10 true
12 true
11 false
13 false
#include <string>
using namespace std;
bool solution(int x) {
bool answer = true;
string str=to_string(x);
int sum=0;
for(char i : str)
{
sum+=(i-'0');
}
if(x%sum==0) return 1;
return 0;
}
#include <string>
using namespace std;
bool solution(int x) {
bool answer = true;
int nTemp = x;
int nSum = 0;
while (nTemp > 0)
{
nSum += nTemp % 10;
nTemp /= 10;
}
return x % nSum == 0 ? true : false;
}
while (n > 0)
T += n % 10; <- push_back을 사용하면 자리별로 벡터값에 저장할 수 있다.
n /= 10;
숫자형 자리수를 한개씩 분해하는 코드로 유용하게 사용되는것 같다. 익혀두면 편할 것 같다.
가장 긴 변의 길이는 다른 두 변의 길이의 합보다 작아야 합니다.
삼각형의 세 변의 길이가 담긴 배열 sides이 매개변수로 주어집니다. 세 변으로 삼각형을 만들 수 있다면 1, 만들 수 없다면 2를 return하도록 solution 함수를 완성해주세요.
제한사항
sides의 원소는 자연수입니다.
sides의 길이는 3입니다.
1 ≤ sides의 원소 ≤ 1,000
입출력 예
sides result
[1, 2, 3] 2
[3, 6, 2] 2
[199, 72, 222] 1
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> sides) {
int answer = 0;
sort(sides.begin(), sides.end());
if(sides[2]<sides[0]+sides[1]) return 1;
else return 2;
}
문제 설명
문자열 s를 숫자로 변환한 결과를 반환하는 함수, solution을 완성하세요.
제한 조건
s의 길이는 1 이상 5이하입니다.
s의 맨앞에는 부호(+, -)가 올 수 있습니다.
s는 부호와 숫자로만 이루어져있습니다.
s는 "0"으로 시작하지 않습니다.
입출력 예
예를들어 str이 "1234"이면 1234를 반환하고, "-1234"이면 -1234를 반환하면 됩니다.
str은 부호(+,-)와 숫자로만 구성되어 있고, 잘못된 값이 입력되는 경우는 없습니다.
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
int answer = 0;
answer = stoi(s);
return answer;
}