負数を取って余剰を取る.

3287 ワード

テーマ1043:Day of Week
時間制限:1秒
メモリ制限:32メガ
特殊問題:いいえ
提出:3481
解決:1291
タイトルの説明:
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400. For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap. Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
入力:
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
出力:
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.
サンプル入力:
9 October 2001
14 October 2001

サンプル出力:
Tuesday
Sunday

ヒント:
Month and Week name in Input/Output: January, February, March, April, May, June, July, August, September, October, November, December Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
ソース:
2008年上海交通大学コンピュータ研究生気試験の本題
質疑応答:
問題を解くのに問題がありますか.解題の心得を分かち合いますか?このトピックについては、次のトピックを参照してください.http://t.jobdu.com/thread-7767-1-1.html
上の問題は区間問題で、前処理で解決できます.
問題は、負数の剰余(型取りMOD)を単純に正数にしてから剰余を取ることができないことです.
#include 
#include 

using namespace std;

#define ISLEAP(x) x % 4==0 && x%100!=0||x%400==0? 1:0

int dayOfMonth[32][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};

string week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

struct Date{
	int year;
	int month;
	int day;
	void nextDay(){
		day++;
		if(day>dayOfMonth[month][ISLEAP(year)]){		
			day=1;
			month++;
			if(month>12){			
				month=1;
				year++;	
			}	
		}			
	}
};

map monthToInt;


int abs(int x)
{
	return x>0?x:-x;
}

int offset[5001][13][32];

int main(void)
{
	monthToInt.insert(pair("January",1));
	monthToInt.insert(pair("February",2));
	monthToInt.insert(pair("March",3));
	monthToInt.insert(pair("April",4));
	monthToInt.insert(pair("May",5));
	monthToInt.insert(pair("June",6));
	monthToInt.insert(pair("July",7));
	monthToInt.insert(pair("August",8));
	monthToInt.insert(pair("September",9));
	monthToInt.insert(pair("October",10));
	monthToInt.insert(pair("November",11));
	monthToInt.insert(pair("December",12));
	Date tmp;
	int d1,y1;
	string m1;
	
	tmp.year=0;
	tmp.month=1;
	tmp.day=1;
	int cnt=0;
	
	while(tmp.year<5001){
		offset[tmp.year][tmp.month][tmp.day]=cnt++;
		tmp.nextDay();
	}
	
	while(cin>>d1>>m1>>y1){
		int days=offset[y1][monthToInt[m1]][d1]-offset[2014][10][13]+1;
		cout<