오답노트
1] 3달이용권에 대한 생각을 잘못해, 설계도 잘못했다.
1월 7월 8월 이렇게 구매권을 사용할 수 있을 줄 알았는데, 그게 아니었다.
그래서 처음에 설계할땐, 0인거는 제외하고 배열을 백트래킹을 이용해 생성하는 방식을 택했는데, 나중에 잘못되었음을 알았다.
문제를 제대로 이해한 후에는 12달 모두 구매권을 사는 방식을 택했다.
0인 것은 어차피 계산하는 부분에서 제외시키면 되니까...
그런데 여기서 3달짜리 계산을 할 때 많이 고통스러웠다.
구현 기초가 부족함을 뼈저리게 느꼈다.
package swexpertmoitest;
import java.util.Scanner;
public class 수영장 {
public static int t,dayPrice,monthPrice,threeMonthPrice,yearPrice;
public static int[] schedule = new int[12];
public static int[] modifiedSchedule;
public static int[] plan = new int[12];
public static int min=Integer.MAX_VALUE;
public static int cnt=0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
t = scan.nextInt();
for(int i=1;i<=t;i++)
{
dayPrice = scan.nextInt();monthPrice = scan.nextInt();threeMonthPrice = scan.nextInt();yearPrice = scan.nextInt();
for(int j=0;j<12;j++) {schedule[j] = scan.nextInt();}
f(0);
if(min>yearPrice) {System.out.println("#"+i+" "+yearPrice);}
else
{
System.out.println("# "+i+" "+min);
}
min=Integer.MAX_VALUE;
}
scan.close();
}
private static void f(int depth) {
if(depth==plan.length)
{
// System.out.println(Arrays.toString(plan));
calculate();
cnt+=1;
return;
}
for(int i=1;i<=3;i++)
{
plan[depth]=i;
f(depth+1);
}
}
private static void calculate() {
int oneDayTicketCnt=0;int oneMonthTicketCnt=0;int threeMonthTicketCnt=0;
for(int i=0;i<12;i++)
{
if(plan[i]==1)
{
oneDayTicketCnt+=schedule[i];
}
if(plan[i]==2)
{
oneMonthTicketCnt+=1;
}
}
boolean flag=false;
int s=0;
threeMonthTicketCnt=0;
for(int j=0;j<12;j++)
{
if(plan[j]==3)
{
if(!flag)
{
s=j;
flag=true;
threeMonthTicketCnt+=1;
}
}
if(j-s==2) {flag=false;s=0;}
}
int money = threeMonthTicketCnt*threeMonthPrice+oneDayTicketCnt*dayPrice+oneMonthTicketCnt*monthPrice;
if(min>money) {min=money;}
}
}
'알고리즘' 카테고리의 다른 글
보호필름[모의문제] (0) | 2019.09.17 |
---|---|
재귀 유용한 틀(1) (0) | 2019.09.16 |
대표값 (0) | 2019.08.13 |
미로 탐색 (0) | 2019.08.09 |
유기농 배추 (0) | 2019.08.09 |