18岁生日
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27702 Accepted Submission(s): 8923
Problem Description
Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他想请你帮忙计算一下他和他的几个朋友从出生到达18岁生日所经过的总天数,让他好来比较一下。
Input
一个数T,后面T行每行有一个日期,格式是YYYY-MM-DD。如我的生日是1988-03-07。
Output
T行,每行一个数,表示此人从出生到18岁生日所经过的天数。如果这个人没有18岁生日,就输出-1。
Sample Input
1 1988-03-07
Sample Output
6574
这个题目其实挺简单的,但是要求考虑全面,以前就遇见过不少次这种生日计算天数的题目,考虑不全面肯定会wa几次,本来这个题目不想贴的,但是鉴于wa了两次才ac,上次比赛这种题目还没能独立ac,就贴个代码,算是警示一下自己。
分开情况考虑初末两年,和中间的17年就ok了。
#include<iostream>
using namespace std;
int main()
{
int t,year,month,day;
char c;
cin>>t;
while(t--)
{
cin>>year>>c>>month>>c>>day;
int sum=0;
//生日不存在的情况
if((year%4==0&&year%100!=0||year%400==0)&&!((year+18)%4==0&&(year+18)%100!=0||(year+18)%400==0))
if(month==2&&day==29)//闰年多出来的一天
{
cout<<-1<<endl;
continue;
}
//生日存在
if((year%4==0&&year%100!=0||year%400==0)&&month<=2)
sum++;
if(((year+18)%4==0&&(year+18)%100!=0||(year+18)%400==0)&&month>=2)
sum++;
sum+=365;
for(int i=year+1;i<year+18;i++)//一共12年
{
sum+=365;
if(i%4==0&&i%100!=0||i%400==0)//闰年
sum++;
}
cout<<sum<<endl;
}
}
What day is it
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4968 Accepted Submission(s): 1468
Problem Description
Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?
Input
There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).
Output
Output one line.
if the date is illegal, you should output "illegal". Or, you should output what day it is.
if the date is illegal, you should output "illegal". Or, you should output what day it is.
Sample Input
2007 11 17
Sample Output
Saturday
额额,这个还不能用菜勒公式,还不能用一个表示每个月多少天的数组吗,心情郁闷,先放了,用两个数组就两个数组吧,,,
#include<iostream>
#include<cstdio>
using namespace std;
int a[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
int b[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
char str[7][20]={"Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday"};
bool run(int year)
{
if((year%4==0 && year%100!=0) || year%400==0)
return true;
return false;
}
int main()
{
int year,month,day;
while(~scanf("%d%d%d",&year,&month,&day))
{
if(run(year))
{
if(day > a[month] || month == 0 || day == 0)
{
cout << "illegal" << endl;
continue;
}
}
else
{
if(day > b[month] || month == 0 || day == 0)
{
cout << "illegal" << endl;
continue;
}
}
int sum=0;
for(int i=1;i<year;i++)//以前所有年
{
if(run(i))
sum+=366%7;
else
sum+=365%7;
}
//当前年
for(int i = 0; i < month; ++i)
{
if(run(year))
sum += a[i];
else
sum += b[i];
sum %= 7;
}
sum+=day;
printf("%s\n",str[sum%7]);
}
return 0;
}