题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4551
题意:已知 出生月份和出生日子的最大公约数; 出生月份和出生日子的最小公倍数; 出生年份;求出生的日子?
注意题目中隐藏的问题。
参考代码:
#include <stdio.h>
int mon[]={0,31,0,31,30,31,30,31,31,30,31,30,31};
int leap_year(int y)
{
if((y%4==0 && y%100)||(y%400==0))
return 1;
return 0;
}
int gcd(int a,int b)
{
return a?gcd(b%a,a):b;
}
int main()
{
int t,i,j,x,y,z,num=0,ans,a,b,c,d;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&x,&y,&z);
if(leap_year(z))
mon[2]=29;
else
mon[2]=28;
ans=0;
for(i=1;i<=12;i++)
{
for(j=1;j<=mon[i];j++)
{
c=gcd(i,j);
d=i*j/c;
if(c==x && d==y)
{
a=i;
b=j;
ans++;
}
}
}
num++;
printf("Case #%d: ",num);
if(ans==1)
printf("%d/%02d/%02d\n",z,a,b);
else if(ans==0)
printf("-1\n");
else
printf("1\n");
}
return 0;
}
备注:
int gcd(int a,int b)
{
return a?gcd(b%a,a):b;
}
这一段代码相当于:
int gcd(int a,int b)
{
int temp;
while(b)
{
temp=b;
b=a%b;
a=temp;
}
return a;
}