본문 바로가기
728x90
반응형
SMALL

분류 전체보기161

[프로그래머스] 주사위의 개수 #include #include using namespace std; int solution(vector box, int n) { int answer = 1; for(int i:box) answer *= i/n; return answer; } 2025. 9. 5.
[프로그래머스] 가위 바위 보 #include #include using namespace std; string solution(string rsp) { string answer = ""; //가위 2, 바위 0, 보 5 for(char hand : rsp){ if(hand == '0') answer+='5'; else if(hand == '2') answer+='0'; else if(hand == '5') answer+='2'; } return answer; } 해당 케이스는 짧아서 괜찮지만 'hand'와 같은 조건이 많아지면 switch~case문을 사용하는것이 효율적인 측면에서 좋음 2025. 9. 5.
[프로그래머스] 개미 군단 #include #include using namespace std; int solution(int hp) { // 장군개미 5 // 병정개미 3 // 일개미 1 vector hps = {5,3,1}; int jang = hp/hps[0]; int byeung = (hp-jang*hps[0])/hps[1]; int il = (hp-jang*hps[0]-byeung*hps[1])/hps[2];; return jang + byeung + il; } * 간단하게 다음과 같이 변경 가능#include #include using namespace std;int solution(int hp) { // 장군개미 5 // 병.. 2025. 9. 5.
[프로그래머스] 직각삼각형 출력하기 #include #include using namespace std;int main(void) { int n; cin >> n; for(int i=1;i#include using namespace std;int main(void) { int n; cin >> n; for(int i=0;i 2025. 9. 4.
[프로그래머스] 옷가게 할인 받기 #include #include using namespace std; int solution(int price) { if(price>=500000) return price*0.8; else if(price>=300000 && price else if(price>=100000 && price else return price; } 2025. 9. 4.
[프로그래머스] 모음 제거 #include #include #include using namespace std;bool is_vowel(unsigned char c){ switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': return true; default: return false; }}string solution(string my_string) { my_string.erase( remove_if(my_string.begin(), my_string.end(), [](u.. 2025. 9. 4.
[프로그래머스] 중복된 숫자 개수 #include #include using namespace std; int solution(vector array, int n) { int answer = 0; for (auto a:array) if(a==n)answer++; return answer; } 2025. 9. 4.
[프로그래머스] 특정 문자 제거하기 #include #include #include using namespace std; string solution(string my_string, string letter) { string answer = ""; char target = letter.front(); for(char str:my_string) if(str!=target) answer+=str; return answer; } 2025. 9. 4.
[프로그래머스] 제곱수 판별하기 #include #include using namespace std; int solution(int n) { double sqr_d = sqrt(n); int sqr_i = static_cast(sqr_d); if(sqr_i==sqr_d) return 1; else return 2; } 2025. 9. 4.
728x90
반응형
LIST