ZOJ - 3326 - An Awful Problem 模拟

来源:互联网 发布:淘宝香港买家收货地址 编辑:程序博客网 时间:2024/05/16 15:15

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

题意:输入T组数据,每组数据包含两个时间,时间的格式是年、月、日(yyyy mm dd),计算两个时间之间所有月份和日期均为素数的天数,包含起始和结束的两天。

解析:主要是素数的判断和闰年的判断,每年,每月符合条件的天数是确定的,一年中的素数月只有2,3,5,7,11;闰年中符合要求的天数为53,平年中符合要求的天数为52;可以将起始的月份和结束的月份单独判断,其他的月份或是年份可以直接计算,需要注意的是平年和闰年的2月份的天数的差别,往往容易被忽略。

根据以上的思路,然后写成代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;bool prime(int a){    bool x=0;    if(a==1) return 0;    for(int i=2; i<=sqrt(a); i++)    {        if(a%i==0)        {            x=1;            break;        }    }    if(x) return 0;    else return 1;}bool leapyear(int a){    if(a%400==0||a%4==0&&a%100!=0) return 1;    else return 0;}int mon(int y,int a){    if(a==3||a==5||a==7) return 31;    else if(a==2&&leapyear(y)) return 29;    else if(a==2&&leapyear(y)==0) return 28;    else if(a==11) return 30;}int main(){    int t,i,j,k,a,b,c,d,e,f;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);        int cnt=0;        if(a==d)        {            if(b==e)            {                if(prime(b))                    for(k=c; k<=f; k++)                    {                        if(prime(k))                            cnt++;                    }            }            else            {                if(prime(b))                {                    for(j=c; j<=mon(a,b); j++)                    {                        if(prime(j))                        {                            cnt++;                        }                    }                }                for(j=b+1; j<=e-1; j++)                {                    if(j==3||j==5||j==7) cnt+=11;                    else if(j==11||j==2&&leapyear(a)) cnt+=10;                    else if(j==2&&leapyear(a)==0) cnt+=9;                }                if(prime(e))                {                    for(j=1; j<=f; j++)                    {                        if(prime(j))                        {                            cnt++;                        }                    }                }            }        }        else        {            if(prime(b))            {                for(j=c; j<=mon(a,b); j++)                {                    if(prime(j))                    {                        cnt++;                    }                }            }            for(j=b+1; j<=12; j++)            {                if(j==3||j==5||j==7) cnt+=11;                else if(j==11||j==2&&leapyear(a)) cnt+=10;                else if(j==2&&leapyear(a)==0) cnt+=9;            }            for(j=a+1; j<=d-1; j++)            {                if(leapyear(j))cnt+=53;                else cnt+=52;            }            if(prime(e))            {                for(j=1; j<=f; j++)                {                    if(prime(j))                    {                        cnt++;                    }                }            }            for(j=1; j<=e-1; j++)            {                if(j==3||j==5||j==7) cnt+=11;                else if(j==11||j==2&&leapyear(d)) cnt+=10;                else if(j==2&&leapyear(d)==0) cnt+=9;            }        }        printf("%d\n",cnt);    }    return 0;}
0 0
原创粉丝点击