码迷,mamicode.com
首页 > 其他好文 > 详细

cpp 1124 显示星期几

时间:2015-04-20 18:38:02      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:日期处理   cpp   

题目链接:http://cpp.zjut.edu.cn/ShowProblem.aspx?ShowID=1124


题面:

显示星期几 
Time Limit:1000MS  Memory Limit:32768K

Description:

针对输入的日期,输出对应的星期几。一个星期的每天对应字串"Mon.","Tus.","Wed.",""Thr.","Fri.","Sat.","Sun."。

Input:

第一个整数N表示后面有N个日期。其日期结构为“YYYY-MM-DD”。

Output:

根据每个日期输出英文压缩星期几的名字。

Sample Input:

3
2006-04-22
2006-04-28
2006-03-01

Sample Output:

Sat.
Fri.
Wed.



题意:就不赘述了。


代码:

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
	int n;
	cin>>n;
	string s;
	while(n--)
	{
	  cin>>s;
	  int year=(s[0]-'0')*1000+(s[1]-'0')*100+(s[2]-'0')*10+(s[3]-'0');
	  int month=(s[5]-'0')*10+(s[6]-'0');
	  int d=(s[8]-'0')*10+(s[9]-'0');
	  int x=(s[2]-'0')*10+(s[3]-'0');
	  int y=(x/4+x)%7;
	  int m;
	  if(month!=2&&month!=1)
	  {
  		if(month==5)m=0;
  		else if(month==8)m=1;
  		else if(month==3||month==11)m=2;
  		else if(month==6)m=3;
  		else if(month==9||month==12)m=4;
  		else if(month==4||month==7)m=5;
  		else m=6;
      }	
      else 
      {
      	if((year%4==0&&year%100!=0)||year%400==0)
      	{
	      if(month==1)m=5;
		  else m=1;	
        }
        else
        {
        	if(month==2)m=2;
        	else m=6;
        }
      }
      int c=(d+m+y)%7;
      if(c==0)cout<<"Sun."<<endl;
      else if(c==1)cout<<"Mon."<<endl;
      else if(c==2)cout<<"Tus."<<endl;
      else if(c==3)cout<<"Wed."<<endl;
      else if(c==4)cout<<"Thr."<<endl;
      else if(c==5)cout<<"Fri."<<endl;
      else if(c==6)cout<<"Sat."<<endl;
	}
}


cpp 1124 显示星期几

标签:日期处理   cpp   

原文地址:http://blog.csdn.net/david_jett/article/details/45151817

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!