zoj 3326

ZOJ - 3326

Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

 Status

Description

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

水题一道,这是我的第一篇博客,因为当时做的时候没出,觉得很憋屈,然后回来以后就搞出来了,我想知道30行是怎么AC的!!

思路:就是单独处理开始的一个月和最后一个月,让这两个月分别变成完整的一个月,然后单独处理所有月份。

#include<iostream>
using namespace std;
int p[34]={0};
void prime()
{
    p[0]=p[1]=1;
    int t;
    for(int i = 2;i <= 31;i++ )
    {
        if(p[i]==0)
        {
            t = 2;
            while(t*i<=31)
            {
                p[t*i] = 1;
                t++;
            }
        }
    }
}//素数表
int runnian(int i)
{
    if((i%4==0&&i%100!=0)||i%400==0)
        return 1;
    else
        return 0;
}//判断闰年
int main()
{
    int n;
    int sy,sm,sd,ey,em,ed;
    int d = 0,m,y;
    prime();
    cin >> n;
    while(n--)
    {
        int sum = 0;
        cin>>sy>>sm>>sd>>ey>>em>>ed;
        for(int i = 1;i < sd;i++)
        {
            if(!p[sm]&&!p[i])
                sum--;
        }//特殊处理第一个月
        if(em==1||em==3||em==5||em==7||em==8||em==10||em==12)
            d = 31;
        else if(em == 4||em==6||em==9||em==11)
            d = 30;
        else if(em == 2)
        {
            if(runnian(ey))
                d=29;
            else
                d=28;
        }
        for(int i = ed+1;i<=d;i++)
        {
            if(!p[em]&&!p[i])
                sum--;
        }<span style="font-family: Arial, Helvetica, sans-serif;">//特殊处理最后一个月</span>
        m = sm;
        y = ey;
        while(1)
        {
            if(m==3||m==5||m==7)
                sum+=11;
            else if(m==11)
                sum+=10;
            else if(m==2)
            {
                if(runnian(sy))
                    sum+=10;
                else
                    sum+=9;
            }
            m++;
            if(sy==y)
            {
                if(m-1==em)
                    break;
            }
            if(m==13)
            {
                m=1;
                sy++;
            }
        }
    cout << sum << endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值