初学,用了个笨办法:各位给看看,提提建议:
# include <stdio.h>
int main()
{
int a,b,c,d,gcd1,gcd2,lcm1,lcm2,sum1,sum2;
int gcd(int num1,int num2);
int lcm(int num1,int num2);
scanf("%d %d",&a,&b);
scanf("%d %d",&c,&d);
gcd1=gcd(a,b);
gcd2=gcd(c,d);
lcm1=lcm(a,b);
lcm2=lcm(c,d);
sum1=gcd1+gcd2;
sum2=lcm1+lcm2;
printf("%d\n",sum1);
printf("%d\n",sum2);
return 0;
}
int gcd(int num1,int num2)
{
int temp,i,gcd;
if(num1<num2)
{temp=num2,num2=num1;num1=temp;}
for (i=num2;i>0;i--)
if(num1%i==0 && num2%i==0)
{gcd=i;
break;}
return (gcd);
}
int lcm(int num1,int num2)
{
int temp,i,lcm;
if(num1<num2)
{temp=num2,num2=num1;num1=temp;}
for (i=num1;;i++)
if(i%num1==0 && i%num2==0)
{lcm=i;
break;}
return (lcm);
}