But when I upload to Judgement.
I got WA#1.
I don't know where is the problem ?
I must check this program later.
According this program there have some trick point.
int day=4; /* Key point */
The day must be 4 first.
1201. Which day is it? Time Limit: 1.0 second Memory Limit: 16 MB Sometimes it is of great importance to know which day of the week a given date will be. And we start searching for the nearest calendar. Being lucky we may find one. And find out that this one does not contain the date you need. What a pity! Thus you are asked to create a calendar that will be able to process any given date in the years range from 1600 till 2400. Given a date, your program should print (see the examples below) a correct calendar for the month containing the date. Do not forget about the leap years. A year is considered to be leap if it is multiple of 4 except it is multiple of 100 except it is multiple of 400. For example 1996 is a leap year, 1900 is not a leap year (it is a multiple of 4 and multiple of 100) and 2000 is a leap year (it is a multiple of 4, multiple of 100 and multiple of 400 as well). Input The first line of input contains a date, i.e. three integer numbers: day (1–31), month (1–12) and year (1600–2400) separated by spaces. Output The output should contain exactly 7 lines with the correct calendar for the month containing the given date. Format of a calendar is given by the examples below (for a reading convenience spaces in output example are replaced with dots, real output should contain spaces instead). And do not forget to highlight the given date by square brackets. Samples input output 16 3 2002
1 3 2002
|
#include<stdio.h>
#include<stdlib.h>
int is_leap_year(int y){
return ( ( y%4==0 ) && (y%100!=0||y%400==0) );
}
int days_per_year(int y){
if(is_leap_year(y))
return 366;
else
return 365;
}
int days_per_month(int m,int y){
const int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(m!=2)
return days[m-1];
else if(is_leap_year(y))
return 29;
else
return 28;
}
int get_day_of_week(int m,int d,int y){
<font color=red> int day=4; /* Key point */ </font>
int i;
for(i=1600;i<y ; i++) day+=days_per_year(i);
for(i=1;i<m;i++) day+=days_per_month(i,y);
day+=d;
return day%7;
}
void print_calendar(int m,int d,int y)
{
const char* day_name[7]={"mon","tue","wed","thu","fri","sat","sun"};
int days=days_per_month(m,y);
int day_begin=1-get_day_of_week(m,1,y);
int more_print = (day_begin + days)%7;
int i,j,flag;
flag = 0;
for(i=0;i<7;i++){
printf("%s",day_name[i]);
for(j=day_begin+i ; j <= days + more_print ; j+=7 )
if( j<1 || j>days )
printf("....");
else if(j==d){
flag = 1;
if(j<10)
printf("..[%d]",j);
else
printf("..[%2d]",j);
}
else{
if(j<day_begin+7){
if(flag){
printf("..%d",j);
flag = 0 ;
}
else{
printf("...%d",j);
}
}
else if(j >= day_begin+7 && j<10){
if(flag){
printf("...%d",j);
flag = 0 ;
}
else{
printf("....%d",j);
}
}
else if(j>=10 && j<= days){
if(flag){
printf("..%2d",j);
flag = 0 ;
}
else{
printf("...%2d",j);
}
}
else{
printf("....");
}
}/* if( j<1 || j>days ) */
printf("\n");
}/* for(i=0;i<7;i++) */
}
int main()
{
int d,m,y;
scanf("%d %d %d",&d,&m,&y);
if( y < 1600 || y > 2400) exit(0);
if( m < 1 || m > 12 ) exit(0);
if( d < 1 || d > 31 ) exit(0);
print_calendar(m,d,y);
exit (0);
}
0 意見:
張貼留言