코딩테스트/프로그래머스 - LV 0
[프로그래머스] 수학, 문자열, 해시, 완전탐색, 조건문
웅드
2024. 1. 5. 19:25
- 개미 군단
#include <string>
#include <vector>
using namespace std;
int solution(int hp) {
int answer = 0;
answer += hp /5;
hp %= 5;
answer += hp / 3;
hp %= 3;
answer += hp;
return answer;
}
- 모스부호(1)
#include <string>
#include <vector>
#include <map>
using namespace std;
map<string,char> mos = {
{".-",'a'},{"-...",'b'},{"-.-.",'c'},{"-..",'d'},{".",'e'},{"..-.",'f'},
{"--.",'g'},{"....",'h'},{"..",'i'},{".---",'j'},{"-.-",'k'},{".-..",'l'},
{"--",'m'},{"-.",'n'},{"---",'o'},{".--.",'p'},{"--.-",'q'},{".-.",'r'},
{"...",'s'},{"-",'t'},{"..-",'u'},{"...-",'v'},{".--",'w'},{"-..-",'x'},
{"-.--",'y'},{"--..",'z'}
};
string solution(string letter) {
string answer = "";
string index = "";
vector<string> s;
for(int i = 0; i < letter.size(); i++){
if(letter[i]==' '){
s.push_back(index);
index="";
}
else{
index += letter[i];
}
}
s.push_back(index);
for(int i=0; i< s.size(); i++){
if(mos.find(s[i])!= mos.end()){
answer += (char)mos[s[i]];
}
}
return answer;
}
- 가위 바위 보
#include <string>
#include <vector>
#include <map>
using namespace std;
string solution(string rsp) {
string answer = "";
for(int i=0; i< rsp.size(); i++){
if(rsp[i]=='2'){
answer += '0';
}
else if(rsp[i]=='0'){
answer += '5';
}
else {
answer +='2';
}
}
return answer;
}
- 구슬을 나누는 경우의 수
#include <string>
#include <vector>
using namespace std;
int factorial(int n){
if(n==1) return 1;
return n * factorial(n-1);
}
int solution(int balls, int share) {
long answer = 1;
int n = (balls-share) < share ? balls-share : share;
if(balls==share) return 1;
for(int i=0; i<n; i++){
answer *= balls-i;
answer /= i+1;
}
return answer;
}
-> 팩토리얼로 풀면 데이터 형 범위 초과로 풀수 없다..
반응형