问题 B: Day of Week
时间限制: 1 Sec 内存限制: 32 MB
题目描述
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.
样例输入
21 December 2012
5 January 2013
样例输出
Friday
Saturday
分析,写了大的c++类,虽然过了,但是代码写的太渣,还不想改了
代码
#include<iostream>
#include<string>
using namespace std;
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
string weekNum[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
string month2Int[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
int NumMonth(string month){
for(int i=1;i<13;i++){
if(month==month2Int[i]) return i;
}
}
class Date{
public:
int year;
int month;
int day;
public:
Date(int year,int month,int day){
this->year=year;
this->month=month;
this->day=day;
}
Date(){}
bool Isleap(){
if(year%4==0&&year%100!=0||year%400==0){
return true;
}else{
return false;
}
}
bool operator==(const Date& d){
if(d.year==this->year&&d.month==this->month&&d.day==this->day){
return true;
}else{
return false;
}
}
bool operator>(const Date& d){
if(this->year>d.year){
return true;
}else if(this->year<d.year){
return false;
}else if(this->month>d.month){
return true;
}else if(this->month<d.month){
return false;
}else{
return this->day>d.day;
}
}
};
void NextDate(Date& date){
if(date.Isleap()){
month[2]=29;//暂时变为29天
}
date.day++;
if(date.day==32&&date.month==12){
date.day=1;
date.month=1;
date.year++;
}else if(date.day>month[date.month]){
date.day=1;
date.month++;
}
month[2]=28;
}
void PreDate(Date& date){//笨 若date>now 互换一下值不就行了。。。
if(date.Isleap()){//不能限制为date.month==2 真正要变的时候是3月1号
month[2]=29;//暂时变为29天
}
date.day--;
if(date.day==0&&date.month==1){
date.day=31;
date.month=12;
date.year--;
}else if(date.day==0){
date.month--;
date.day=month[date.month];
}
month[2]=28;
}
int main(){
Date now=Date(2018,9,25);
int week=2;
Date date;
int d,y;
int dd;
string m;
while(cin>>d>>m>>y){
dd=0;
date=Date(y,NumMonth(m),d);
if(date>now){//>
while(!(date==now)){
PreDate(date);
dd++;
}
}else if(date==now){//==
dd=0;
}else{//<
while(!(date==now)){
NextDate(date);
dd--;
}
}
cout<<weekNum[(week+(dd%7)+7)%7]<<endl;
}
return 0;
}
问题 C: 打印日期
时间限制: 1 Sec 内存限制: 32 MB
题目描述
给出年分m和一年中的第n天,算出第n天是几月几号。
输入
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。
输出
可能有多组测试数据,对于每组数据,按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
样例输入
2013 60
2012 300
2011 350
2000 211
样例输出
2013-03-01
2012-10-26
2011-12-16
2000-07-29
注意y(1<=y<=3000),年份不一定是4位,也要考虑添0
代码
#include<iostream>
using namespace std;
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
int y,n;
while(cin>>y>>n){
if(y%4==0&&y%100!=0||y%400==0){
days[2]=29;
}
int i=1;
while(n>days[i]){
n-=days[i];
i++;
}
//cout<<y<<"-"<<(i<10?"0":"")<<i<<"-"<<(n<10?"0":"")<<n<<endl;//写这个就不过?? 输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。 原来y的范围是1~3000 小于1000时长度不一定4位
printf("%04d-%02d-%02d\n",y,i,n);
days[2]=28;
}
return 0;
}
问题 D: 日期类
时间限制: 1 Sec 内存限制: 32 MB
题目描述
编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作。
输入
输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日。测试数据不会有闰年。
输出
输出m行。按xxxx-xx-xx的格式输出,表示输入日期的后一天的日期。
样例输入
2
1999 10 20
2001 1 31
样例输出
1999-10-21
2001-02-01
B题写的太复杂,不过在这派上用场了
代码:
#include<iostream>
using namespace std;
int Month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
class Date{
public:
int year;
int month;
int day;
public:
Date(int year,int month,int day){
this->year=year;
this->month=month;
this->day=day;
}
Date(){}
void NextDate(){
this->day++;
if(this->day>Month[this->month]){
this->day=1;
this->month++;
if(this->month==13){
this->month=1;
year++;
}
}
}
void show(){
printf("%04d-%02d-%02d\n",this->year,this->month,this->day);
}
};
int main(){
int m,year,month,day;
Date d;
while(cin>>m){
while(m--){
cin>>year>>month>>day;
d=Date(year,month,day);
d.NextDate();
d.show();
}
}
return 0;
}
问题 E: 日期累加
时间限制: 1 Sec 内存限制: 32 MB
题目描述
设计一个程序能计算一个日期加上若干天后是什么日期。
输入
输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。
输出
输出m行,每行按yyyy-mm-dd的个数输出。
样例输入
1
2008 2 3 100
样例输出
2008-05-13
B题写得很复杂,不过后面这些题用B的成果就相当简单了,B的日期类可以当模板了
代码:
#include<iostream>
using namespace std;
int Month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
class Date{
public:
int year;
int month;
int day;
public:
Date(int year,int month,int day){
this->year=year;
this->month=month;
this->day=day;
}
Date(){}
bool Isleap(){
if(year%4==0&&year%100!=0||year%400==0){
return true;
}else{
return false;
}
}
void NextDate(){
if(Isleap()){
Month[2]=29;//暂时变为29天
}
day++;
if(day==32&&month==12){
day=1;
month=1;
year++;
}else if(day>Month[month]){
day=1;
month++;
}
Month[2]=28;
}
void show(){
printf("%04d-%02d-%02d\n",year,month,day);
}
};
int main(){
int m;
int year,month,day,num;
Date d;
while(cin>>m){
while(m--){
cin>>year>>month>>day>>num;
d=Date(year,month,day);
while(num--) d.NextDate();
d.show();
}
}
return 0;
}