반응형
링크
https://school.programmers.co.kr/learn/courses/30/lessons/12926
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제 설명 및 제한 조건
나의 코드
#include <string>
#include <vector>
using namespace std;
string solution(string s, int n) {
string answer = "";
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') //공백
answer += s[i];
else {
if (s[i] < 'a') { //대문자
if (s[i] + n > 'Z')
answer += s[i] + n - 26;
else
answer += s[i] + n;
}
else { //소문자
if (s[i] + n > 'z')
answer += s[i] + n - 26;
else
answer += s[i] + n;
}
}
}
return answer;
}
채점 결과
다수 코드
#include <string>
#include <vector>
using namespace std;
string solution(string s, int n) {
string answer = "";
for(auto &a: s) {
if(islower(a)) {
if(!islower(a+=n%26))
a-=26;
} else if(isupper(a)) {
if(!isupper(a+=n%26))
a-=26;
}
}
return s;
}
반응형
'프로그래머스' 카테고리의 다른 글
[프로그래머스 C++] 문자열을 정수로 바꾸기 (0) | 2022.10.28 |
---|---|
[프로그래머스 C++] 3진법 뒤집기 (0) | 2022.10.23 |
[프로그래머스 C++] 이상한 문자 만들기 (0) | 2022.10.22 |
[프로그래머스 C++] 직사각형 별찍기 (0) | 2022.10.18 |
[프로그래머스 C++] 최대공약수와 최소공배수 (0) | 2022.10.17 |