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)……很水
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 }