2179 - 求lcm和gcd

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

给定两个数求最小公倍数和最大公约数。

题目输入

输入包含多组样例

每行包括两个整数a,b(0<a ,b<=10000);        

题目输出

每行两个数输出两个数,第一个数是最小公倍数,第二个最大公约数

输入/输出样例

输入格式

2 3

输出格式

6 1

C语言解答

#include<stdio.h>
main()
{
 int a,b,num1,num2,temp;
 
 while(scanf("%d %d",&num1,&num2)!=EOF)
 {
 
 if(num1<num2)  
 { temp=num1;
  num1=num2; 
  num2=temp;
 }
a=num1;b=num2;
while(b!=0)/*利用辗除法,直到b为0为止*/
 {
  temp=a%b;
  a=b;
  b=temp;
 }
 printf("%d",num1*num2/a);
printf(" %d",a);

printf("\n");
}
}

C++解答

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>

using namespace std;

int gcd(int a, int b) {
  if (b != 0)
    return gcd(b, a % b);
  else
    return a;
}

int main() {
  //freopen("Problem8.in", "r", stdin);
  //freopen("Problem8.out", "w", stdout);

  int a, b;
  while (scanf("%d %d", &a, &b) != EOF) {
    int c = gcd(a, b);
    printf("%d %d\n", a * b / c, c);
  }

  return 0;
}