카테고리 없음
[프로그래머스] Level 1 Test 2문제
Rowen Jobs
2022. 8. 20. 11:42
728x90
반응형
첫번째 문제.
문자열 s 의 값을 받아서 문자 'p' , 문자 'y' 의 갯수가 같으면 true, 다르면 false 없을 경우에는 true 를 return 하는 문제입니다. (대문자, 소문자 구분없이 집계)
class Solution {
boolean solution(String s) {
boolean answer = true;
int pCnt = 0;
int yCnt = 0;
// 소문자, 대문자 상관없이 갯수를 집계해야하기 때문에 모두 대문자로 치횐
s = s.toUpperCase();
for(int i = 0; i < s.length(); i++){
s.charAt(i);
if(s.charAt(i) == 'P'){
pCnt++;
}else if(s.charAt(i) == 'Y'){
yCnt++;
}
}
if(pCnt == yCnt){
answer = true;
}else{
answer = false;
}
return answer;
}
}
두번째 문제
정수 n 의 약수를 구하여 약수의 총 합을 return 해주는 문제였습니다.
class Solution {
public int solution(int n) {
int answer = 0;
int a;
// n 이하의 정수들을 for문을 실행시켜 n 과 나눈 나머지가 0 인 정수 a 만 answer의
// 값에 더해주었습니다.
for(a = 1; a <= n; a++){
if((n % a) == 0){
answer += a;
}
}
return answer;
}
}
728x90