問題 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;
}