usaco: friday the thirteen what's wrong with my logic?

前端 未结 2 471
难免孤独
难免孤独 2021-01-24 14:47

The question asks to calculate the number of 13ths that fall on each day of the week. This is my code.

class CopyOffriday {
public static void main(String[] args         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-24 15:31

    As pointed out in comments, you are iterating for 11 months. I tried this problem after you posted to know if I am missing something. Other than that it looks okay. Let us know if 12 months fixes the problem. Otherwise I'll try looking more into your code.

    SPOILER ALERT for my referred code.

     HashMap daysInAMonth = new HashMap();
        daysInAMonth.put(0, 31);
        daysInAMonth.put(2, 31);
        daysInAMonth.put(3, 30);
        daysInAMonth.put(4, 31);
        daysInAMonth.put(5, 30);
        daysInAMonth.put(6, 31);
        daysInAMonth.put(7, 31);
        daysInAMonth.put(8, 30);
        daysInAMonth.put(9, 31);
        daysInAMonth.put(10, 30);
        daysInAMonth.put(11, 31);
    
    
        HashMap dayFrequency = new HashMap(7);
        //sat - 0
        //      sun -1
    //      mon -2
    //      tue -3
    //      wed -4
    //      thu -5 
    //      fri -6
    
        dayFrequency.put(0, 0);
        dayFrequency.put(1, 0);
        dayFrequency.put(2, 0);
        dayFrequency.put(3, 0);
        dayFrequency.put(4, 0);
        dayFrequency.put(5, 0);
        dayFrequency.put(6, 0);
    
    
    
        int N = Integer.parseInt(st.nextToken());
        if (N==0) {
            out.println("0 0 0 0 0 0 0");
            System.exit(0);
        }
        System.out.println(N);
        int firstFriday = 0;
        int lastFriday = 0;
        for(int i=0 ;i

提交回复
热议问题