https://programmers.co.kr/learn/courses/30/lessons/12954
입출력예시
class Solution {
public long[] solution(int x, int n) {
long[] answer = new long[n];
for (int i =0; i<n; i ++){
answer[i] = x *(long)(i+1);
}
return answer;
}
}
x=2, n=5일 때 answer값은 2,4,6,8,10이 되게 만들어야하는문제
1. 예를 들어 x가 2이고 n이 3이면 , 2씩 3번 증가하는 값을 answer에 넣으면 된다.
2. 따라서 n길이 만큼 반복문을 이용해 x씩 증가하는 값을 answer에 넣는다.
3. x는 -10000000 이상, 10000000 이하인 정수이기 때문에 i를 long으로 형변환 한다.
https://programmers.co.kr/learn/courses/30/lessons/82612
제한사항
입출력 예시
class Solution {
public long solution(int price, int money, int count) {
long answer = 0;
long total = 0;
for (int i=1; i<=count; i++){
total +=price*i;
if(money < total){
answer = total - money;
} else {
answer = 0;
}
}
return answer;
}
}
price=3, money=20, count=4일때 총 이용금액은 price를 count의 길이만큼 곱해서 더해주고 money가 모자라면 얼마가 모자라는지 , 남으면 0을 리턴해야한다
1.총 이용요금을 나타내기위해 long total 을 선언하고 0으로초기화
2.count가 0이 될수는 없으므로 i=1부터 count값이 될때까지 for문을 돌려 그값을 price에 곱해서 total에 저장
예를들어 price =3 ,count =4 면 for문은 1,2,3,4 이렇게 네번 돌아가게되고 각 1,2,3,4 를 3에 곱해서 다더해주면 3,6,9,12,가 나오게되고 이것을 total에 다 더해주면 total은 30이된다
3. money < total 이면 total - money를 해서 answer에 넣어주고 아닐땐 0 으로 반환
https://programmers.co.kr/learn/courses/30/lessons/12901
제한조건
입출력 예
class Solution {
public String solution(int a, int b) {
String[] day = {"FRI", "SAT", "SUN", "MON", "TUE", "WED", "THU"};
int[] date = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int today = 0;
5 24
for(int i=0; i <a-1; i++){
today += date[i];
}
today += b -1;
return day[today%7];
}
}
a는 월이고, b는 일이다.
String배열에 금요일부터 모든 요일을 다 넣어 놨다.(문제 설명에서 보다시피 첫날이 금요일 이기 때문)
다음 int 배열에는 달마다 마지막 날을 넣어 놨다.(1월은 31일, 2월은 윤년이니 29일....)
today는 몇 월 며칠을 전부 더해준 값이다
for문을 돌려 a-1까지 date 배열에 있는 값을 today에 더해준다.(7월이라면 6월까지는 다 더하는 것)
그다음 select에 b-1의 값을 더해준다.
b-1을 하는 이유는 1월 1일이 하루가 지난날이 아니기 때문이다.
만약 a, b에 1,1이 들어오고 b-1을 해주지 않는다면 day [today]는 day [1]이 되어 버리고 그러면 1월 1일은 금요일인데 SAT가 나오게 된다.
마지막으로 리턴할 때 today의 값을 요일 수 7로 나눈 나머지를 day에서 리턴 시켜 준다.
https://programmers.co.kr/learn/courses/30/lessons/12910
제한사항
입출력 예
import java.util.*;
class Solution {
public int[] solution(int[] arr, int divisor) {
int[] answer = {};
ArrayList<Integer> list = new ArrayList<>();
for(int i =0; i<arr.length; i ++){
if(arr[i] % divisor ==0){
list.add(arr[i]);
}
}
if(list.isEmpty()){
list.add(-1);
}
answer = new int[list.size()];
for (int i =0; i<list.size(); i ++){
answer[i] =list.get(i);
}
Arrays.sort(answer);
return answer;
}
}
ArrayList길이가 1 이상일 때 answer배열에 ArrayList 배열을 대입 후 정렬
ArrayList길이가 0일 때 -1 대입
https://programmers.co.kr/learn/courses/30/lessons/70128
제한사항
입출력 예