zoukankan      html  css  js  c++  java
  • 武汉科技大学ACM :1007: 华科版C语言程序设计教程(第二版)例题4.13

    Problem Description

     输入两个整数,求他们的最大公约数和最小公倍数。

    Input

     两个整数。

    Output

     最大公约数和最小公倍数。

    Sample Input

    12 9

    Sample Output

    3 36

    HINT

     可以把求最小公约数和最小公倍数写成函数,方便以后调用。

     1 #include <stdio.h>
     2 
     3 void main()
     4 
     5 {
     6 
     7          int m,n;
     8 
     9          while(scanf("%d%d",&m,&n)!=EOF)
    10 
    11          {
    12 
    13                    if(m>n)
    14 
    15                    {
    16 
    17                             for(int i=m;;i--)
    18 
    19                             {
    20 
    21                                      if((m%i==0)&&(n%i==0))
    22 
    23                                      {
    24 
    25                                                printf("%d %d
    ",i,m*n/i);
    26 
    27                                                break;
    28 
    29                                      }
    30 
    31                             }
    32 
    33                    }
    34 
    35                    else
    36 
    37                    {
    38 
    39                             for(int i=n;;i--)
    40 
    41                             {
    42 
    43                                      if((m%i==0)&&(n%i==0))
    44 
    45                                      {
    46 
    47                                                printf("%d %d
    ",i,m*n/i);
    48 
    49                                                break;
    50 
    51                                      }
    52 
    53                             }
    54 
    55                    }
    56 
    57          }
    58 
    59 }

    其他代码:

     1 #include<stdio.h>
     2 #include<math.h>
     3 int gcd(int a,int b)
     4 {
     5     if(a%b==0)
     6         return b;
     7     else
     8         return gcd(b,a%b);
     9 }
    10 int lcm(int a,int b)
    11 {
    12     return a*b/gcd(a,b);
    13 }
    14 int main()
    15 {
    16     int a,b;
    17     while(~scanf("%d%d",&a,&b))
    18     {
    19         printf("%d %d
    ",gcd(a,b),lcm(a,b));
    20     }
    21     return 0;
    22 }
  • 相关阅读:
    Spring.NET学习笔记
    开源项目地址
    委托的实现匿名函数和朗姆达表达式
    c#事件与委托
    c# 时间戳转换
    List 排序
    DDD的好文章
    【转】理解JMeter聚合报告(Aggregate Report)
    【转】JMeter 通过 JDBC 访问 Oracle 和 MySQL
    【转】使用JMeter测试你的EJB
  • 原文地址:https://www.cnblogs.com/liuwt365/p/4154162.html
Copyright © 2011-2022 走看看