2013
12-12

# 九度-1043-Day of Week[解题代码]

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

9 October 2001
14 October 2001


Tuesday
Sunday

Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

java 代码如下：
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {

static String weeks[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
static String months[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
public static int getM(String str){
for(int i=0; i<12; i++){
if(str.equals(months[i]))
return i;
}
return -1;
}
public static boolean isYear(int y){
return (y%4 == 0 && y % 100 !=0) || y % 400 == 0;
}
static int DaysOfMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNextInt()){
int d = s.nextInt();
int month = getM(s.next());
int year = s.nextInt();
//Calendar c  = new GregorianCalendar(year, month, d);
int days = (year-1) * 365;
for(int i=1; i<year; i++)
if(isYear(i)) days++;
for(int i=0; i<month; i++)
days += DaysOfMonth[i];
days += d;
if(month > 2 && isYear(year))
days++;
//System.out.println(c.get(Calendar.YEAR) + " " + c.get(Calendar.MONTH) + " " + c.get(Calendar.DATE));
System.out.println(weeks[days%7]);
}

}

}
/**************************************************************
Problem: 1043
User: coder
Language: Java
Result: Accepted
Time:90 ms
Memory:17216 kb
****************************************************************/

cpp 代码如下：

#include <stdio.h>
#include <string.h>

char Week[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char Month[13][20] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int DaysOfMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int leapYear(int y)
{
return ((y%4==0 && y%100!=0) || y%400==0);
}

int main()
{
int date, year, nmonth, i, days;
char cmonth[20];
while(~scanf("%d %s %d", &date, cmonth, &year))
{
for(i=1; i<=12; i++)
{
if(strcmp(cmonth, Month[i])==0)
{
nmonth = i;
break;
}
}
days = (year - 1) * 365;
for(i=1; i<year; i++)
{
if(leapYear(i))
days += 1;
}
for(i=1; i<nmonth; i++)
days += DaysOfMonth[i];
days += date;
if(nmonth>2 && leapYear(year))
days += 1;
puts(Week[days%7]);
}
return 0;
}
/**************************************************************
Problem: 1043
User: coder
Language: C++
Result: Accepted
Time:0 ms
Memory:1020 kb
****************************************************************/