思路是把平面看成无边界的。
刚开始算lcm来做,超时了
#include <iostream>
#include <cstdio>
using namespace std;
int lcm(int x,int y)
{
int i;
for(i=max(x,y);i<=x*y;i++)
if(i%x==0&&i%y==0)
break;
return i;
}
int main()
{
int a,b,c,p,q,ans;
while( scanf("%d%d",&a,&b) != EOF )
{
c=lcm(a,b);
p=c/a;
q=c/b;
ans=p+q-2;
if(p%2&&q%2) cout<<"C ";
else if(p%2&&q%2==0) cout<<"B ";
else if(p%2==0&&q%2) cout<<"D ";
else cout<<"A ";
cout<<ans<<endl;
}
return 0;
}
用lcd:
#include <iostream>
#include <cstdio>
using namespace std;
int gcd( int a,int b )
{
return b?gcd(b,a%b):a;
}
int main()
{
int a, b;
while( scanf("%d%d",&a,&b) != EOF )
{
int c = gcd( a, b );
int k1 = b / c, k2 = a / c;
int result = k1 + k2 - 2;
if( k1 % 2 && k2 % 2 ) printf("C ");
else if( k1 % 2 && ! (k2%2) ) printf("B ");
else if( !(k1%2) && k2 % 2 ) printf("D ");
else printf("A ");
printf("%d\n",result);
}
return 0;
}
两者可以转换的原因:
lcm*gcd=a*b;
所以lcm/a=b/gcd;
lcm/b=a/gcd;