SGU 115: Calendar

Jasonlin posted @ 2011年3月31日 14:24 in SGU with tags 日期 , 2264 阅读

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=115

题目大意:

     输出2001年的某天的星期几。

解题思路:

      只要注意2001年的二月是28天,还要判断日期是否合法。

解题代码:

#include<iostream>
using namespace std;
bool ok(int n,int m){
	if(m<=0||m>12) return false;
	if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12))
		if(n<=0||n>31) return false;
	if((m==4)||(m==6)||(m==9)||(m==11))
		if(n<=0||n>30) return false;
	if(m==2)
		if(n<=0||n>28) return false;
	return true;
}
int day(int n,int m){
	return n+31*((m>1)+(m>3)+(m>5)+(m>7)+(m>8)+(m>10)) \
			+30*((m>4)+(m>6)+(m>9)+(m>11))+28*(m>2);
}
int main(){
	int n,m;
	while(cin>>n>>m){
		if(ok(n,m)){
			int w=day(n,m)%7;
			if(w) cout<<w<<endl;
			else cout<<7<<endl;
		}
		else
			cout<<"Impossible"<<endl;
	}
	return 0;
}

扩展知识:http://en.wikipedia.org/wiki/Leap_year

  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter