首页 > 代码库 > zoj3326An Awful Problem
zoj3326An Awful Problem
题目链接:
点我点我
题目:
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 */