• 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 }
  • 相关阅读:
    积性函数前缀和
    CF1067D Computer Game
    Atcoder Tenka1 Programmer Contest 2019 题解
    Codeforces Round #549 (Div. 1) 题解
    SHOI2019旅游记
    CF871D Paths
    CF1065E Side Transmutations
    停更公告
    博客说明
    SCOI2019酱油记
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2882376.html
Copyright © 2020-2023  润新知