close

將每個月的天數儲存起來

算出當天是在整年的第幾天

再%7即可得出答案

不需要自己想餘數是0 == 星期幾

直接看題目的sample input ouput(1/6是星期四) 再在指標陣列中依序排列即可(2017-05-23: 指標陣列->陣列中每個element皆是pointer 每個pointer points to a string literal)

網址:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3170&mosmsg=Submission+received+with+ID+17404674

------------------------------------------------------------------------

#include <stdio.h>

int main()
{
    const int day_of_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const char *day_of_week[] = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
    int _case, month, day, i, total_day;

    scanf("%d", &_case);
    while(_case--)
    {
        scanf("%d%d", &month, &day);
        total_day = 0;
        for(i = 0; i < month; i++)
            total_day += day_of_month[i];
        total_day += day;
        printf("%s\n", day_of_week[total_day % 7]);
    }
    return 0;
}
 

 

-2017-05-23---------------------------------------------------------------------

package test;

import java.util.*;

public class main {

    public static void main(String[] args) {
        try {
            final Scanner in = new Scanner(System.in);
            final int[] months = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            final String[] dayOfTheWeek = {"Friday", "Saturday",
                "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"}; // 1/1 => Saturday
            int cases = in.nextInt();
            while (cases-- > 0) {
                int mon = in.nextInt();
                int day = in.nextInt(), dayOfTheYear = 0;
                for (int i = 0; i < mon; i++) {
                    dayOfTheYear += months[i];
                }
                dayOfTheYear += day;
                dayOfTheYear %= 7; // A week 7 days
                System.out.println("" + dayOfTheWeek[dayOfTheYear]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

charPointer.png

arrow
arrow
    全站熱搜

    awesq123 發表在 痞客邦 留言(0) 人氣()