ZOJ 3326 An 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
这个题 是昨天省赛训练的水题 ,当时没想到那么麻烦还有那么多细节,直接啪啪啪扯了两百多行,太纠结了 ,越做越麻烦,改成调用函数也晚了,今天拿出来题册
搜到这个题目 ,用函数再来一遍确实思路和调试难易程度明显提升。不过中间还是出现了好多不顺畅的地方,傻到竟然把9当成素数了。感觉写的技巧性还行~,其实lastyear 和firstyear这两个函数体内还可以再减化,唉,毕竟该睡觉了!太晚了。
#include<iostream>
using namespace std;
bool leapyear(int x)
{
if(x%4==0&&x%100!=0||(x%400==0))
return 1
;
return 0;
}
bool prime(int y)
{
if(y==2)
return 1;
int i;
for( i=2; i<y; i++)
{
if(y%i==0)
break;
}
if(i==y)
return 1;
return 0;
}
int firstyear(int ys,int ms,int ds)
{
int num=0;
if(ms==2)
{
for(int k=ds; k<=29; k++)
{
if(prime(k))
{
num++;
}
}
if(!leapyear(ys))
num--;
}
if(ms==3||ms==5||ms==7||ms==11)
{
for(int k=ds; k<=31; k++)
if(prime(k))
num++;
if(ms==11)
num--;
}
for(int j=ms+1; j<=12; j++)
{
if(j==2)
{
num+=10;
if(!leapyear(ys))
num--;
}
if(j==3||j==5||j==7||j==11)
{
num+=11;//cout<<"nn="<<num<<endl;
if(j==11)
{
num--;//cout<<"num="<<num<<endl;
}
}
}
return num;
}
int lastyear(int ye,int me,int de)
{
int num=0;
if(me==2)
{
for(int k=1; k<=de; k++)
{
if(prime(k))
{
//cout<<"k="<<k<<"";
num++;
}
}
}
if(me==3||me==5||me==7||me==11)
{
for(int k=1; k<=de; k++)
if(prime(k))
num++;
}
for(int j=1; j<me; j++)
{
if(j==2)
{
num+=10;
if(!leapyear(ye))
num--;
}
if(j==3||j==5||j==7||j==11)
{
num+=11;
if(j==11)
num--;
}
}
return num;
}
int main()
{
int t,ys,ms,ds,ye,me,de,num;
cin>>t;
while(t--)
{
num=0;
cin>>ys>>ms>>ds>>ye>>me>>de;
if(ys==ye)
{
int num1=lastyear(ye,me,de);
int num2=lastyear(ys,ms,ds);
num=num1-num2;
if(prime(ms)&&prime(ds)&&prime(me)&&prime(de))
cout<<num+1<<endl;
else
cout<<num<<endl;
continue;
}
num+=firstyear(ys,ms,ds);//cout<<num<<" - "<<endl;
num+=lastyear(ye,me,de);//cout<<num<<endl;
ys++;
while(ys<ye)
{
//cout<<"leapyear="<<leapyear(ys)<<endl;
if(leapyear(ys))
num+=53;
else
num+=52;
ys++;
//cout<<"num="<<num<<endl;
};
cout<<num<<endl;
}
return 0;
}
/***
2
1000 01 01 2999 12 31
1000 11 03 1000 11 03
******/

浙公网安备 33010602011771号