nefuoj-1036:2月29日

description

<span style="font-family:SimSun;font-size:18px;">給定兩個日期,計算這兩個日期之間有多少個2月29日(包括起始日期)。

只有閏年有2月29日,滿足以下一個條件的年份爲閏年:

1. 年份能被4整除但不能被100整除

2. 年份能被400整除</span>

input

<span style="font-family:SimSun;font-size:18px;">第一行爲一個整數T,表示數據組數。

之後每組數據包含兩行。每一行格式爲&quot;month day, year&quot;,表示一個日期。

month爲{&quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;, &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot; , &quot;December&quot;}中的一個字符串。

day與year爲兩個數字。

數據保證給定的日期合法且第一個日期早於或等於第二個日期。</span>

output

<span style="font-family:SimSun;font-size:18px;">對於每組數據輸出一行,形如&quot;Case #X: Y&quot;。X爲數據組數,從1開始,Y爲答案。
</span>

sample_input

<span style="font-family:SimSun;font-size:18px;">2
January 12, 2012
March 19, 2012
August 12, 2899
August 12, 2901</span>

sample_output

<span style="font-family:SimSun;font-size:18px;">Case #1: 1
Case #2: 0</span>


題解:因爲這是14屆大一的綜合賽的題,所以我就順便做了一下,這一道題是第一題,水題,模擬就行了,就是有點麻煩。。


code:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;


int isrun(int n)//runnian
{
    if(n%400==0) return 1;
    if(n%4==0&&n%100!=0)
    return 1;
    return 0;
}
int main()
{
    char month[12][15]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"};
    int t;
    char m1[15],m2[15];
    int d1,d2,y1,y2,mn1,mn2;
    int ans;
    int c=1;
    cin>>t;
    while(t--)
    {
        ans=0;
        scanf("%s %d,%d",m1,&d1,&y1);
        getchar();
        scanf("%s %d,%d",m2,&d2,&y2);
        getchar();
        printf("Case #%d: ",c++);
        for(int i=0; i<12; i++)
        {
            if(strcmp(m1,month[i])==0)
            {
                mn1=i+1;
            }
            if(strcmp(m2,month[i])==0)
            {
                mn2=i+1;
            }
        }
        //cout<<mn1<<' '<<mn2;//
        if(y1==y2&&mn1<=2&&mn2>2)//判斷同一年不同月份;
        {
            isrun(y1)? cout<<"1"<<endl: cout<<"0"<<endl;
            continue;
        }
        if(y1==y2&&mn1<=2&&mn2==2)//判斷同一年同一月份;
        {
            d2==29? cout<<"1"<<endl: cout<<"0"<<endl;
            continue;
        }
        if(y1<y2)//判斷不同年;
        {
            if(mn1<=2&&isrun(y1))
            ans=1;
            else
            ans=0;
            for(int i=y1+1; i<y2; i++)
            if(isrun(i)) ans++;

            if(isrun(y2)&&(mn2>2||d2==29))
            ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


下邊是當初比賽公佈出來的代碼,不是我寫的哦~~

code2:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char mon[12][10]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"};
int main()
{
    int t,T=1;
    scanf("%d",&t);
    char month[2][10],c;
    int m,d,y,m1,d1,y1;
    int cnt;
    while(t--)
    {
        cnt=0;
        scanf("%s",month[0]);
        scanf("%d%c%d",&d,&c,&y);
        scanf("%s",month[1]);
        scanf("%d%c%d",&d1,&c,&y1);
        for(int i=0;i<12;i++)
          if(strcmp(month[0],mon[i])==0)
          {
              m=i+1;
              break;
          }
        for(int i=0;i<12;i++)
          if(strcmp(month[1],mon[i])==0)
          {
              m1=i+1;
              break;
          }
        if(y==y1)
        {
            if((y%400==0)||(y%4==0&&y%100!=0))
              if((m==1||(m==2&&d<=29))&&(m1>2||(m1==2&&d1==29)))
                cnt++;
        }
        else
        {
            if((y%400==0)||(y%4==0&&y%100!=0))
              if((m==1)||(m==2&&d<=29)) cnt++;
            cnt+=(y1-1)/4-(y1-1)/100+(y1-1)/400-(y/4-y/100+y/400);
            if((y1%400==0)||(y1%4==0&&y1%100!=0))
              if(m1>2||(m1==2&&d1==29)) cnt++;
        }
        printf("Case #%d: %d\n",T++,cnt);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章