zoukankan      html  css  js  c++  java
  • sicily 1623. Sixth Grade Math

    Description
    In sixth grade, students are presented with different ways to calculate the Least Common Multiple (LCM) and the Greatest Common Factor (GCF) of two integers. The LCM of two integers a and b is the smallest positive integer that is a multiple of both a and b . The GCF of two non-zero integers a and b is the largest positive integer that divides both a and b without remainder.
    Input
    The first line of input contains a single integer N , (1≤N≤1000) which is the number of data sets that follow. Each data set consists of a single line of input containing two positive integers, a and b , (1≤a, b≤1000) separated by a space.
    Output
    For each data set, you should generate one line of output with the following values: The data set number as a decimal integer (start counting at one), a space, the LCM, a space, and the GCF.

    水题,GCF其实就是GCD,最大公约数,LCM最小公倍数有一个公式lcm(a,b) = a*b/gcd(a,b)……很水

    View Code
     1 #include<stdio.h>
     2 int gcf( int a, int b )
     3 {
     4     if( a == 0)
     5         return b;
     6     if ( b == 0 )
     7         return a;
     8     return gcf( b, a % b );
     9 
    10 }
    11 
    12 int lcm( int a, int b )
    13 {
    14     return a * b / gcf(a, b);
    15 }
    16 int main()
    17 {
    18     int n, i, a, b;
    19     
    20     scanf("%d", &n);
    21     for ( i = 1; i <= n; i++ )
    22     {
    23         scanf("%d %d", &a, &b);
    24         printf("%d %d %d\n", i, lcm(a, b), gcf(a, b));
    25     }
    26     
    27     return 0;
    28 }
  • 相关阅读:
    hdu1754 I Hate It
    51nod 1174 1174 区间中最大的数
    51nod1305 Pairwise Sum and Divide
    51nod 1622 集合对[算法马拉松19 C]
    51nod1265 四点共面
    51nod 1244 莫比乌斯函数之和
    51nod 1240 莫比乌斯函数
    51nod 1113 矩阵快速幂
    51nod 1264 线段相交(几何)
    51nod 1412 AVL树的种类(dp)
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2882376.html
Copyright © 2011-2022 走看看