Created
August 22, 2020 12:08
-
-
Save minhphong306/1d19ea492af2b010ed7d8e776884bf33 to your computer and use it in GitHub Desktop.
Rút gọn phân số
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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