zoj3326An Awful Problem

题目链接:

点我点我

题目:

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

时间: 2024-07-28 12:50:50

zoj3326An Awful Problem的相关文章

ZOJ 3326 An Awful Problem (较清晰写法,附详细注解)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3326 题面: 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 w

ZOJ 3326 An Awful Problem 模拟

只有在 Month 和 Day 都为素数的时候才能得到糖 那就模拟一遍时间即可. //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cmath> #include <sta

We have a problem with promises

原文地址:http://fex.baidu.com/blog/2015/07/we-have-a-problem-with-promises/ 用Javascript的小伙伴们,是时候承认了,关于 promises 我们一直存在着问题.并非说 promises 本身有问题,Promises/A+ 是极好的. 就我过去数年观察大量 PouchDB API 以及其他 promise-heavy API 的使用者们与这些 API 的搏斗中我发现,最大的问题是: 大部分使用 promises 的小伙伴们

【转】We have a problem with promises

这是我看的自认为最好的一篇讲解如何使用Promise的文章,原文地址:http://fex.baidu.com/blog/2015/07/we-have-a-problem-with-promises/ 用Javascript的小伙伴们,是时候承认了,关于 promises 我们一直存在着问题.并非说 promises 本身有问题,Promises/A+ 是极好的. 就我过去数年观察大量 PouchDB API 以及其他 promise-heavy API 的使用者们与这些 API 的搏斗中我发

A Math Problem

A Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 237    Accepted Submission(s): 117 Problem Description You are given a positive integer n, please count how many positive integers

Water Problem

water problem 发布时间: 2015年10月10日 15:34   时间限制: 1000ms   内存限制: 256M 描述 题意很简单 给你N个数, Q个查询 每次查询给你一个区间[L, R] 你要找出 [L, R] 这个区间里面取模M后的最大值. 输入 第一行一个T,表示测试数据组数.第二行两个整数N, M (1<=N<=10^5, 1<=M<=10^9).第三行给你N个整数 整数范围在1到10^9之间.第四行给你一个整数Q. ( 1<=Q<=10^5)

FOJ Problem 2261 浪里个浪

                                                                                                                                                           Problem 2261 浪里个浪 Accept: 40    Submit: 106Time Limit: 1500 mSec    Memory Limit : 32768 KB Pro

XJTUOJ wmq的A&#215;B Problem FFT

wmq的A×B Problem 发布时间: 2017年4月9日 17:06   最后更新: 2017年4月9日 17:07   时间限制: 3000ms   内存限制: 512M 描述 这是一个非常简单的问题. wmq如今开始学习乘法了!他为了训练自己的乘法计算能力,写出了n个整数,并且对每两个数a,b都求出了它们的乘积a×b.现在他想知道,在求出的n(n−1)2个乘积中,除以给定的质数m余数为k(0≤k<m)的有多少个. 输入 第一行为测试数据的组数. 对于每组测试数据,第一行为2个正整数n,

hidden node and exposed node problem

Exposed node problem In wireless networks, theexposed node problem occurs when a node is prevented from sending packets to other nodes because of a neighboring transmitter. Consider an example of 4 nodes labeled R1, S1, S2, and R2, where the two rece