Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Created August 22, 2020 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minhphong306/1d19ea492af2b010ed7d8e776884bf33 to your computer and use it in GitHub Desktop.
Save minhphong306/1d19ea492af2b010ed7d8e776884bf33 to your computer and use it in GitHub Desktop.
Rút gọn phân số
package main
import "fmt"
func ucln(a int, b int) int {
if a%b == 0 {
return b
}
return ucln(b, a%b)
}
func main() {
// Tử và mẫu nhập vào. Chỗ này mình mock luôn cho ngắn
tu := 10
mau := 15
if tu % mau == 0 {
fmt.Println(tu/mau)
return
}
uc := ucln(tu, mau)
tu = tu / uc
mau = mau / uc
if mau < 0 {
mau *= -1
tu *= -1
}
fmt.Printf("%v/%v", tu, mau)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment