日期计算
1、给定年份以及天数,输出是?月?日
#include<iostream>  
using namespace std;    
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};    
int main()  
{  
    int year,day;  
    cin>>year>>day;  
    if((year%4==0&&year%100!=0)||(year%400==0))  
            month[1]=29;  
     int mon=1;  
    for(int i=0;i<12;i++)
     {  
         if(day>month[i])
          {  
            mon++;  
            day-=month[i];  
           }  
        else
               break;   //不要忘了
      }  
    cout<<mon<<" "<<day<<endl;  
    return 0;  
}  
2、求两个日期的差值
#include<stdio.h>
int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};
bool isleap(int year)
{
    return  (year%4==0&&year%100!=0)||(year%400==0);
} 
int main()
{
  int time1,y1,m1,d1;
  int time2,y2,m2,d2;
  while(scanf("%d%d",&time1,&time2)!=EOF)
   {
      if(time1>time2)
      {
	 int temp=time1;
	    time1=time2;
	    time2=temp;
      }
	 y1=time1/10000,m1=time1%10000/100,d1=time1%100;
	 y2=time2/10000,m2=time2%10000/100,d2=time2%100;
	 int ans=1;
     while(y1<y2||m1<m2||d1<d2)
      {
    	 d1++;
        if(d1==month[m1][isleap(y1)]+1)
    	 {
    	    m1++;
    	    d1=1;
	 }
	 if(m1==13)
	 {
	    y1++;
	    m1=1;
	  }
	   ans++;
	 }
	 printf("%d\n",ans);
 }
  return 0;
}
3、给出年月日,求是第几天
#include<iostream>
using namespace std;
 
const int ya[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
const int yb[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
 
int main()
{
    int y, m, d;
    while (cin >> y >> m >> d)
    {
        int sum = 0;
        for (int i = 0; i < m - 1; ++i)
        {
            if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
                sum += yb[i];
            else
                sum += ya[i];
        }
        sum += d;
        cout << sum << endl;
    }
     
    return 0;
}
4、给定年月日,输出是周几
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main() 
{ 
    int year,month,day; 
    int total; 
    cin>>year>>month>>day; 
    if(month<3)
   { 
        month+=12; 
        year-=1; 
    } 
total=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7; 
switch(total) 
 { 
    case(0): 
                   cout<<"MON"; 
                   break; 
    case(1): 
                   cout<<"TUE"; 
                   break; 
    case(2): 
                   cout<<"WED"; 
                   break; 
    case(3): 
                   cout<<"THU"; 
                   break; 
    case(4): 
                   cout<<"FRI"; 
                   break; 
    case(5): 
                  cout<<"SAT"; 
                  break; 
    case(6): 
                  cout<<"SUN"; 
                  break; 
} 
}
//或者:
int func(int y, int m, int d)    /* 0 = Sunday */
    {
     static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
     y -= m < 3;
     return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
    }
                                    
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号