题目:输入两个正整数m和n,求其最大公约数和最小公倍数

分析:使用欧几里德算法

package main
import (
    "fmt"
)
/*最大公约数*/
func euclidean(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    }
    return a
}
/*最小公倍数*/
func leastcommmultiple(a, b, c int) int {
    return (a / c) * (b / c) * c
}
func main() {
    var m, n, k int
    fmt.Scanf("%d %d", &m, &n)
    k = euclidean(m, n)
    fmt.Printf("%d和%d的最大公约数是%d\n", m, n, k)
    fmt.Printf("%d和%d的最小公倍数是%d\n", m, n, leastcommmultiple(m, n, k))
}
运行结果:

48 95

48和95的最大公约数是1

48和95的最小公倍数是4560

...

40 91

40和91的最大公约数是1

40和91的最小公倍数是3640

...

324 958

324和958的最大公约数是2

324和958的最小公倍数是155196

package main
import (
    "fmt"
)
/*最大公约数*/
func euclidean(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    }
    return a
}
/*最小公倍数*/
func leastcommmultiple(a, b, c int) int {
    return (a / c) * (b / c) * c
}
func main() {
    var m, n, k int
    fmt.Scanf("%d %d", &m, &n)
    k = euclidean(m, n)
    fmt.Printf("%d和%d的最大公约数是%d\n", m, n, k)
    fmt.Printf("%d和%d的最小公倍数是%d\n", m, n, leastcommmultiple(m, n, k))
}
package main
import (
    "fmt"
)
/*最大公约数*/
func euclidean(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    }
    return a
}
/*最小公倍数*/
func leastcommmultiple(a, b, c int) int {
    return (a / c) * (b / c) * c
}
func main() {
    var m, n, k int
    fmt.Scanf("%d %d", &m, &n)
    k = euclidean(m, n)
    fmt.Printf("%d和%d的最大公约数是%d\n", m, n, k)
    fmt.Printf("%d和%d的最小公倍数是%d\n", m, n, leastcommmultiple(m, n, k))
}
package main
import (
    "fmt"
)
/*最大公约数*/
func euclidean(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    }
    return a
}
/*最小公倍数*/
func leastcommmultiple(a, b, c int) int {
    return (a / c) * (b / c) * c
}
func main() {
    var m, n, k int
    fmt.Scanf("%d %d", &m, &n)
    k = euclidean(m, n)
    fmt.Printf("%d和%d的最大公约数是%d\n", m, n, k)
    fmt.Printf("%d和%d的最小公倍数是%d\n", m, n, leastcommmultiple(m, n, k))
}
package main
import (
    "fmt"
)
/*最大公约数*/
func euclidean(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    }
    return a
}
/*最小公倍数*/
func leastcommmultiple(a, b, c int) int {
    return (a / c) * (b / c) * c
}
func main() {
    var m, n, k int
    fmt.Scanf("%d %d", &m, &n)
    k = euclidean(m, n)
    fmt.Printf("%d和%d的最大公约数是%d\n", m, n, k)
    fmt.Printf("%d和%d的最小公倍数是%d\n", m, n, leastcommmultiple(m, n, k))
}





09-26 08:02