#UVa:12019-Doom’s Day Algorithm

先記住1月0號(如果有0號的話)是禮拜幾,接下來以1月0號開始跳至所輸入當天是禮拜幾即可得解。

C++(0.008)

/*******************************************************/
/* UVa 12019 - Doom's Day Algorithm                    */
/* Author: LanyiKnight [at] knightzone.studio             */
/* Version: 2012/07/15                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int n;
  int M, D;
  const int ZERO_DAY = 5;
  const int MONTH_DAYS[] = { 0, 31, 28, 31, 30, 31, 30, 
                            31, 31, 30, 31, 30, 31 };
  const string DAYS[] = { "Sunday", "Monday", "Tuesday", "Wednesday",
                          "Thursday", "Friday", "Saturday" };
  while( scanf( "%d", &n ) != EOF ){
    for( int i = 0 ; i < n ; i++ ){
      scanf( "%d%d", &M, &D );
      D += ZERO_DAY;
      D %= 7;
      for( int j = 1 ; j < M ; j++ ){
        D += MONTH_DAYS[j] % 7;
        D %= 7;
      }
      printf( "%s\n", DAYS[D].c_str() );
    }
  }
  return 0;
}
廣告

Comment

There is no comment on this post. Be the first one.

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

%d 位部落客按了讚: