1. 程式人生 > >CCF 日期計算

CCF 日期計算

問題描述
試題編號: 201509-2
試題名稱: 日期計算
時間限制: 1.0s
記憶體限制: 256.0MB
問題描述: 問題描述   給定一個年份y和一個整數d,問這一年的第d天是幾月幾日?
  注意閏年的2月有29天。滿足下面條件之一的是閏年:
  1) 年份是4的整數倍,而且不是100的整數倍;
  2) 年份是400的整數倍。 輸入格式   輸入的第一行包含一個整數y,表示年份,年份在1900到2015之間(包含1900和2015)。
  輸入的第二行包含一個整數d,d在1至365之間。 輸出格式   輸出兩行,每行一個整數,分別表示答案的月份和日期。 樣例輸入 2015
80 樣例輸出 3
21 樣例輸入 2000
40 樣例輸出 2
9

AC程式碼:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int month[13] ={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    int year,sum;
    scanf("%d%d",&year,&sum);
    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        month[2]++;
    int i;
    for(i=1; i<=12; i++)
    {
        if(sum >= month[i])
        {
            sum -= month[i];
            if(sum == 0)
            {
                sum = month[i];
                break;
            }
        }
        else
            break;
    }
    printf("%d\n",i);
    printf("%d\n",sum);
    return 0;
}