数学趣题——求两个数的最大公约数和最小公倍数

   1: #include <stdio.h>
   2:  
   3: int Gcd(int a, int b)
   4: {
   5:     int min;
   6:     if (a <= 0 || b <= 0)
   7:         return -1;
   8:     if (a > b)
   9:         min = b;
  10:     else
  11:         min = a;
  12:  
  13:     while (min)
  14:     {
  15:         if (a % min == 0 && b % min == 0)
  16:             return min;
  17:  
  18:         min --;
  19:     }
  20:  
  21:     return -1;
  22: }
  23:  
  24: int Lcm(int a, int b)
  25: {
  26:     int max;
  27:     if (a <= 0 || b <= 0)
  28:         return -1;
  29:     if (a > b)
  30:         max = a;
  31:     else
  32:         max = b;
  33:  
  34:     while (max)
  35:     {
  36:         if (max % a == 0 && max % b == 0)
  37:             return max;
  38:  
  39:         max++;
  40:     }
  41:  
  42:     return -1;
  43: }
  44:  
  45: int main()
  46: {
  47:     int a, b;
  48:     printf("Input a b\n");
  49:     scanf("%d %d", &a, &b);
  50:  
  51:     printf("the gcd is %d\n", Gcd(a, b));
  52:     printf("the lcm is %d\n", Lcm(a, b));
  53:     return 0;
  54: }

你可能感兴趣的:(数学)