2022.11.24일 9문제 완료 기록.
- 짝수는 싫어요
class Solution {
public int[] solution(int n) {
int k=0;
int[] answer;
if(n%2==0){
answer = new int [n/2];
}else{
answer = new int [n/2+1];
}
for(int i=0;i<=n;i++){
if(i%2==1){ //홀수
answer[k]=i;
k++;
}
}
return answer;
}
}
- 배열의 유사도
import java.util.*;
class Solution {
public int solution(String[] s1, String[] s2) {
int answer = 0;
for(int i=0;i<s1.length;i++){
for(int j=0;j<s2.length;j++){
if(s1[i].equals(s2[j])){
answer++;
}
}
}
return answer;
}
}
- 옷가게 할인 받기
class Solution {
public int solution(int price) {
int answer = 0;
if(price >= 500000) {
price *= 0.8;
} else if(price >= 300000) {
price *= 0.9;
} else if(price >= 100000) {
price *= 0.95;
}
return price;
}
}
- 문자열안에 문자열
import java.util.*;
class Solution {
public int solution(String str1, String str2) {
int answer = 0;
if(str1.contains(str2)) {
answer=1;
}else {
answer=2;
}
return answer;
}
}
- 중앙값 구하기
import java.util.*;
class Solution {
public int solution(int[] array) {
int answer = 0;
Arrays.sort(array);
answer=array[(array.length-1)/2];
return answer;
}
}
- 자릿수 더하기
class Solution {
public int solution(int n) {
int answer = 0;
while(n!=0){
answer+=n%10;
n=n/10;
}
return answer;
}
}
- 순서쌍의 개수
class Solution {
public int solution(int n) {
int answer = 0;
for(int i =2;i<=n;i++){
if(n%i==0)
answer++;
}
return answer+1;
}
}
- 모음 제거
class Solution {
public String solution(String my_string) {
String answer = "";
return my_string.replaceAll("a|e|i|o|u", "");
}
}
- 숨어있는 숫자의 덧셈 (1)
class Solution {
public int solution(String my_string) {
my_string = my_string.replaceAll("[^0-9]", "");
int answer = 0;
for(int i=0; i<my_string.length(); i++) {
answer += Integer.parseInt(String.valueOf(my_string.charAt(i)));
}
return answer;
}
}
- 직각삼각형 출력하기
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1;i<=n;i++){
for(int j=1; j<=i;j++ ){
System.out.print("*");
}
System.out.println("");
}
}
}
'코딩테스트 & 알고리즘 및 기타 > 프로그래머스' 카테고리의 다른 글
프로그래머스 코딩테스트 입문 5 (0) | 2022.11.28 |
---|---|
프로그래머스 코딩테스트 입문 4 (0) | 2022.11.27 |
프로그래머스 코딩테스트 입문 2 (0) | 2022.11.23 |
프로그래머스 코딩테스트 입문1 (0) | 2022.11.22 |