码迷,mamicode.com
首页 > 其他好文 > 详细

zoj3326An Awful Problem

时间:2014-07-10 19:29:30      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   strong   os   

题目链接:

点我点我

题目:

An Awful Problem

Time Limit: 1 Second      Memory Limit: 32768 KB

In order to encourage Hiqivenfin to study math, his mother gave him a sweet candy when the day of the month was a prime number. Hiqivenfin was happy with that. But several days later, his mother modified the rule so that he could get a candy only when the day of the month was a prime number and the month was also a prime number. He felt a bit upset because he could get fewer candies. What‘s worse, his mother changed the rule again and he had to answer a question before he could get a candy in those days. The question was that how many candies he could get in the given time interval. Hiqivenfin wanted to cry and asked you for help. He promised to give you half of a candy if you could help him to solve this problem.

Input

There are multiple test cases. The first line of input is an integer T (0 < T <= 50), indicating the number of test cases. Then T test cases follow. The i-th line of the next T lines contains two dates, the day interval of the question. The format of the date is "yyyy mm dd". You can assume both dates are valid. Hiqivenfin was born at 1000-01-01 and would not die after 2999-12-31, so the queries are all in this interval.

Hiqivenfin didn‘t seem to be an earthman, but the calendar was the same as that we usually use. That is to say, you need to identify leap years, where February has 29 days. In the Gregorian calendar, leap years occur in years exactly divisible by four. So, 1993, 1994, and 1995 are not leap years, while 1992 and 1996 are leap years. Additionally, the years ending with 00 are leap years only if they are divisible by 400. So, 1700, 1800, 1900, 2100, and 2200 are not leap years, while 1600, 2000, and 2400 are leap years.

Output

Output the number of candies Hiqivenfin could get in the time interval. Both sides of the interval are inclusive.

Sample Input

2
1000 01 01 1000 01 31
2000 02 01 2000 03 01

Sample Output

0
10

这是一道比较坑的模拟题,我因为把数组开大了,然后又没有判断数组越界,导致我直接wa了很久。。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int year1,year2,month1,month2,day1,day2;
int vis[50];
int a[15]={2,3,5,7,11,13,17,19,23,29,31};
//这里初始化的时候注意31后面要找到一个比31大的或者在43行限制i<=10..
//这里wa哭了。。。
int judge(int x)
{
     if(x==3||x==5||x==7||x==2||x==11)
        return 1;
     return 0;
}

int solve(int standard,int year,int month,int day)
{
    int ans=0;
    for(int i=standard;i<=year-1;i++)
    {
        if((i%4==0&&i%100!=0)||i%400==0)
            ans=ans+53;
        else
            ans=ans+52;
    }
      for(int i=2;i<=month-1;i++)
     {
        if(i==3||i==5||i==7)
            ans=ans+11;
        if(i==11)
            ans=ans+10;
        if(i==2)
        {
            if((year%4==0&&year%100!=0)||year%400==0)
                ans=ans+10;
            else
                ans=ans+9;
        }
     }
    if(month==2||month==3||month==5||month==7||month==11)
    {
       for(int i=0;a[i]<=day&&i<=10;i++)
           ans++;
    }
  //  cout<<"ans"<<ans<<endl;
    return ans;
}

int main()
{
    int t,ans,temp,standard;
    int ans1,ans2;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d%d",&year1,&month1,&day1);
        scanf("%d%d%d",&year2,&month2,&day2);
        standard=year1;
        ans1=solve(standard,year1,month1,day1);
        ans2=solve(standard,year2,month2,day2);
        for(int i=0;i<=11;i++)
        {
            if(a[i]==day1&&judge(month1))
                vis[day1]=1;
        }
        if(vis[day1])
            ans2++;//还有就是如果左端点满足条件的话就ans2++。
        printf("%d\n",ans2-ans1);

    }
    return 0;
}
/*
5
1000 02 02 1000 02 05
1000 02 02 1000 02 06
1000 01 01 1000 01 31
2000 02 01 2000 03 01
1000 03 04 1000 04 23
*/


zoj3326An Awful Problem,布布扣,bubuko.com

zoj3326An Awful Problem

标签:des   style   http   color   strong   os   

原文地址:http://blog.csdn.net/u014303647/article/details/37584333

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!