Mission1 - Calendar만들기
int n = gc.getActualMaximum(Calendar.DATE);
eday.add(Calendar.DATE, -1);
똑같이 달의 마지막 날을 구할 수 있다.
public class CalendarMission {
public static void main(String[] args) {
int START_DAY_OF_WEEK=0;
int END_DAY=0;
Scanner sc = new Scanner(System.in);
System.out.print("보고싶은 년도를 입력해주세요 : ");
int year = sc.nextInt();
System.out.print("보고싶은 달을 입력해주세요 : ");
int month = sc.nextInt();
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.set(year, month-1,1);
end.set(year, month-1, end.getActualMaximum(Calendar.DATE));
START_DAY_OF_WEEK = start.get(Calendar.DAY_OF_WEEK);
END_DAY = end.get(Calendar.DATE);
System.out.println(" \t \t\t<"+year+"년 "+month+"월>");
System.out.println("\t일\t월\t화\t수\t목\t금\t토");
for(int i=1;i<START_DAY_OF_WEEK;i++) {
System.out.print("\t");
}
for(int i=1,n=START_DAY_OF_WEEK;i<=END_DAY;i++,n++) {
System.out.print("\t"+i);
if(n%7==0) System.out.println();
}
}
}
보고싶은 년도를 입력해주세요 : 2020
보고싶은 달을 입력해주세요 : 7
<2020년 7월>
일 월 화 수 목 금 토
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Calendar gc = Calendar.getInstance();
gc.set(2020, 1, 1);
int n = gc.getActualMaximum(Calendar.DATE);
int week = gc.get(Calendar.DAY_OF_WEEK);
System.out.println("<2020년 7월>");
System.out.println("일\t월\t화\t수\t목\t금\t토");
for(int i=1;i<week;i++) {
System.out.print("\t");
}
for(int i=1;i<=n;i++) {
System.out.print(i+"\t");
if((week + i -1)%7 == 0) {
System.out.println();
}
}
Mission2 - 딱지갯수맞추기
[딱지 수집]
영미는 1일부터 N까지 매일 아침 8시에 A개의 딱지를 구매하여
오후 3시에 동생 현준이에게 딱지를 나눠준다.
단. 영미가 가지고 있는 딱지 개수의 범위 내에서 아래와 같은 규칙을 지키기로 했다.
1. 현준이에게는 하루에 P개 이하의 딱지를 준다.
2. 딱지를 나눠주고 남은 딱지가 있다면 보관하고 다음 날 구매한 딱지와 합한다.
현준이는 최종적으로 K개 딱지를 갖고 싶어 하고, 영미도 현준이가 원하는 개수의
딱지를 가능한 빠른 시일 안에 주고 싶다.
현준이가 K개의 딱지를 갖게 되는데 필요한 최소일수를 구하시오.
만약 불가능할 경우 -1을 출력한다.
[입력]
첫 번째 줄에 테스트케이스의 수 T(1<= T<=50)가 주어진다.
각 테스트케이스마다. 첫 번째 줄에는 영미가 딱지를 구매하는 날짜 N,
현준이가 희망하는 딱지 개수 K, 하루에 받을 수 있는 최대 딱지의 양 P가 각각
공백을 두고 주어진다.
두 번째 줄에는 영미가 1일부터 N일까지 매일 구매하는 딱지의 개수 Ai가 공백을
두고 N개 주어진다.(1<=Ai<=100)
[출력]
각 줄마다 #T(T는 테스트케이스 번호)를 출력한 뒤, 현준이가 K개의 딱지를 갖기
위해 필요한 최소 일수를 출력한다. 단 불가능할 경우 -1을 출력한다.
[sample input]
2
2 3 8
1 2
3 17 6
10 10 10
[sample output]
#1 2
#2 3
public class LabelFolded {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("TestCast수 입력 : ");
int t = sc.nextInt();//testcase개수
StringBuffer sb = new StringBuffer(); //대신 string써도됨
for(int i=0;i<t;i++) {
sb.append("#" + (i+1) +" "+result(sc.nextInt(), sc.nextInt(),sc.nextInt()));
sb.append("\n");
}
System.out.println(sb);
}
public static int result(int days, int wish, int max) {
Scanner sc = new Scanner(System.in);
int Ai[] = new int[10];
int sum = 0;
int count = 0;
for(int i=0;i<days;i++) {
Ai[i] = sc.nextInt();
Ai[count++] = sum;
if(max>Ai[i]) {
sum += Ai[i];
}else {
sum += max;
Ai[i+1] = Ai[i]-max;
}
}//for문
if(wish>=sum) {
return count;
}else {
return -1;
}
}
}
TestCast수 입력 : 2
2 3 8
1 2
3 17 6
10 10 10
#1 2
#2 3
public class LabelFoldedLec {
static int t;
static int N, K, P;//N:영미 구매날짜수, K:현준이 희망개수, P:1일 최대지급수
static int A[] = new int[101];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
for (int test_case = 1; test_case <= t; test_case++) {
N = sc.nextInt();
K = sc.nextInt();
P = sc.nextInt();
for(int i=1;i<=N;i++) {
A[i] = sc.nextInt();
}
int Answer = -1;
//연산을 수행
int rem = 0;
for(int i=1;i<=N;i++) {
rem = rem + A[i];
int m = Math.min(P, rem);
K = K - m;
if(K <= 0) {
Answer = i;
break;
}
}
System.out.println("#" + test_case + " " + Answer);
}
}//main
}
**String클래스는 특별히 많이 쓰이기 때문에 별도의 객체생성없이 바로 호출가능하다
String str = account.toString();
System.out.println(str); //이런식으로
하지만 StringBuffer클래스를 사용할때는
new연산자를 통해 객체생성을 해줘야 한다.
버퍼의 길이를 따로 생성해주지 않으면 기본 16개의 문자를 저장할 수 있는 크기의 버퍼를 생성한다.