본문 바로가기
알고리즘/프로그래머스

프로그래머스(C++, JAVA) / level 1 : 문자열 내 p와 y의 개수

by clean_h 2022. 3. 30.
728x90

level 1 : 문자열 내 p와 y 개수

https://programmers.co.kr/learn/courses/30/lessons/12916?language=java 

 

코딩테스트 연습 - 문자열 내 p와 y의 개수

대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를

programmers.co.kr

 

🎯 코드(C++)

#include <string>
#include <vector>
#include <cmath>

using namespace std;

int solution(int n) {
    int answer = 0;
    for(int i=1; i<=sqrt(n); i++){
        if(n%i ==0){
            answer += i;
            if(i != n/i){
                answer += n/i;
            }
        }
    }
    return answer;
}

 

🎯 코드(JAVA)

class Solution {
    public int solution(int n) {
        int answer = 0;
        for(int i=1; i<=Math.sqrt(n); i++){
            if(n%i == 0){
                answer += i;
                if(i != n/i){
                    answer += n/i;
                }
            }
        }
        return answer;
    }
}

 

728x90

댓글