Zoj 2420 calendar stores the struct in the vector

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.