Question address: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 1420
Ideas:
At the beginning, I thought the question was simple, but different months had different days, and I don't know how to solve it easily. Then I thought of the table-making method, and used the information of each day as a struct, which exists in the vector one by one. The angle mark of the vector is auto-incrementing, that is, the number of days in.
Note:
1 leap year judgment
2 The output format 1-1 must be written as 01-01
3. for Super-memory problems, set the three attribute values to short int, and the direct int value will exceed the memory.
4 weeks, because 2000-01-01 is Saturday, the nth day is week (n + 6) % 7.
Code:
#include<iostream>#include<string>#include<vector>using namespace std;struct date{ short year; short month; short day;};bool isleap(int n){ if(n%4==0&&n%100!=0) return 1; if(n%400==0) return 1; return 0;}int shortmonth(int n){ if(n==2||n==4||n==6||n==9||n==11) return 1; else return 0;}int main(){ int n; string * day=new string[7]; day[0]="Monday"; day[1]="Tuesday"; day[2]="Wednesday"; day[3]="Thursday"; day[4]="Friday"; day[5]="Saturday"; day[6]="Sunday"; vector<date> v; for(int i=2000;i<10000;i++) for(int j=1;j<=12;j++) for(int k=1;k<=31;k++) { if(j==2&&isleap(i)&&k>29) continue; if(j==2&&(!isleap(i))&&k>28) continue; if(shortmonth(j)&&k>30) continue; date today; today.year=i; today.month=j; today.day=k; v.push_back(today); } while(cin>>n) { if(n==-1) break; cout<<v[n].year<<"-"; if(v[n].month<10) cout<<"0"; cout<<v[n].month<<"-"; if(v[n].day<10) cout<<"0"; cout<<v[n].day<<" "; cout<<day[(n+5)%7]<<endl; }}